chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Here is my code:

[TestClass,TestFixture]
    public class NotificationInterfaceTestSuite
    {
        [TestMethod,Test,Isolated]
        public void prove_send_message_is_called()
        {
            MockManager.Init();

            MockObject mock_es = MockManager.MockObject<ExchangeService>();
            
            ExchangeService es = (ExchangeService)mock_es.Object;
           

            Mock mock_sender = MockManager.Mock<Sender>();
            Sender s = new Sender();
            Mock mock_server = MockManager.Mock<Server>();
            Server svr = new Server();
           
            Mock mock_user = MockManager.Mock<User>();
            User u = new User();
            Mock mock_ci = MockManager.Mock<CalendarInterfaceAdapter>();
            CalendarInterfaceAdapter ci = new CalendarInterfaceAdapter();

            Mock mock_ji = MockManager.Mock<JobInterface>();

            Mock mock_qa = MockManager.Mock<QueueAdapter>();
            QueueAdapter qa = new QueueAdapter();

            JobInterface ji = new JobInterface(qa,s,true);
            mock_ji.ExpectCall("SendMessage");

            Mock mock_email = MockManager.Mock<EmailMessage>();
            EmailMessage email = new EmailMessage(es);

            Mock mock_addy = MockManager.Mock<EmailAddress>();
            EmailAddress addy = new EmailAddress("foo@bar.com");
            //mock_addy.Strict = true;

            //mock_email.AssignField("Id", new ItemId("TEST_ID"));
            Isolate.WhenCalled(() => email.Id).WillReturn(new ItemId("TEST_ID"));
            Isolate.WhenCalled(() => email.From).WillReturn(addy);
            Isolate.WhenCalled(() => email.Subject).WillReturn("Your snark is a boojum!");

            MockObject mock_eac = MockManager.MockObject(typeof(EmailAddressCollection));
            EmailAddressCollection eac = (EmailAddressCollection)mock_eac.Object;
            mock_eac.ExpectCall("Add");

            mock_email.ExpectGetAlways("ToRecipients",eac);
            mock_email.ExpectGetAlways("CcRecipients", eac);
            mock_email.ExpectGetAlways("BccRecipients", eac);
            mock_email.ExpectGetAlways("ReplyTo", eac);

            eac.Add(addy);

            Isolate.WhenCalled(() => email.ToRecipients).WillReturnCollectionValuesOf(eac);
            Isolate.WhenCalled(() => email.CcRecipients).WillReturnCollectionValuesOf(eac);
            Isolate.WhenCalled(() => email.BccRecipients).WillReturnCollectionValuesOf(eac);
            Isolate.WhenCalled(() => email.ReplyTo).WillReturnCollectionValuesOf(eac);

            NotificationInterface ni = new NotificationInterface(svr, u, ci, es, ji);

            ni.SendNotification(EventType.Created, "TEST_ITEM_ID", email as Item);

            mock_ji.Verify();
        }


And the software under test:

 public void SendNotification(EventType eventType, string itemId, Item i = null)
        {
            NetworkLogging.Instance.Log(">>> SEND NOTIFICATION");
            NetworkLogging.Instance.Log("event type: " + eventType.ToString());
            NetworkLogging.Instance.Log("item id: " + itemId);
            
            Item item;
            if (i != null)
            {
                item = i;
            }
            else
            {
                try
                {
                    item = Item.Bind(service, itemId);
                }
                catch (ServiceResponseException e)
                {
                    NetworkLogging.Instance.Log("******* ERROR GODDAMIT!");
                    NetworkLogging.Instance.Log(e.Message);
                    return;
                }
                catch (ServiceRequestException e)
                {
                    NetworkLogging.Instance.Log("holy **** what is going on?");
                    return;
                }

            }
            if (item is EmailMessage) {
                NetworkLogging.Instance.Log(">>> item is message");
                
                if (eventType != EventType.Created) {
                    NetworkLogging.Instance.Log(">>> not new message");
                    return;
                }

                EmailMessage message = item as EmailMessage;

                Dictionary<string, object> n = new Dictionary<String, object>();
                n["server_id"] = server.Id;
                n["user_id"] = user.Id;
                // We pass the itemId in above, use it if 
                // the item's Id property is unset
                
                n["item_id&quo
asked by ntresch (1.9k points)

3 Answers

0 votes
Hi,

There are 2 Isolator APIs in this test.
Mock API :
Mock mock_sender = MockManager.Mock<Sender>();

AAA API :
Isolate.WhenCalled(() => email.ToRecipients).WillReturnCollectionValuesOf(eac);


You should pick one API (the one you should choose is AAA :wink: ) and use it alone.
The documentation for AAA is available here.

You'll find it much easier to use AAA.

After you do that, use Typemock tracer to track the interaction of fake objects with your code to spot the error.

Please let me know if it helps.
answered by alex (17k points)
0 votes
Sure enough, everything is behaving as I expected it to originally now. Thanks!
answered by ntresch (1.9k points)
0 votes
Hi,

Glad to read that it works :)

BTW, it can be done with even fewer LOCs:

Instead of writing:
Server svr = Isolate.Fake.Instance<Server>(Members.ReturnRecursiveFakes);
User u = Isolate.Fake.Instance<User>(Members.ReturnRecursiveFakes);
CalendarInterfaceAdapter ci = Isolate.Fake.Instance<CalendarInterfaceAdapter>(Members.ReturnRecursiveFakes);
ExchangeService es = Isolate.Fake.Instance<ExchangeService>(Members.ReturnRecursiveFakes);
JobInterface ji = Isolate.Fake.Instance<JobInterface>();

NotificationInterface ni = new NotificationInterface(svr, u, ci, es, ji);


You can use Isolate.Fake.Dependencies like so:
NotificationInterface ni = Isolate.Fake.Dependencies<NotificationInterface>() 
var ji= Isolate.GetFake<JobInterface >(ni); //in order to assert


*Isolate.Fake.Dependencies returns a real object( not a fake ).
answered by alex (17k points)
...