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:

    public class NotificationInterfaceTestSuite
        public void prove_send_message_is_called()

            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 mock_email = MockManager.Mock<EmailMessage>();
            EmailMessage email = new EmailMessage(es);

            Mock mock_addy = MockManager.Mock<EmailAddress>();
            EmailAddress addy = new EmailAddress("");
            //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_email.ExpectGetAlways("CcRecipients", eac);
            mock_email.ExpectGetAlways("BccRecipients", eac);
            mock_email.ExpectGetAlways("ReplyTo", eac);


            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);


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;
                    item = Item.Bind(service, itemId);
                catch (ServiceResponseException e)
                    NetworkLogging.Instance.Log("******* ERROR GODDAMIT!");
                catch (ServiceRequestException e)
                    NetworkLogging.Instance.Log("holy **** what is going on?");

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

                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
asked by ntresch (9.4k points)

3 Answers

0 votes

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

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 (18k points)
0 votes
Sure enough, everything is behaving as I expected it to originally now. Thanks!
answered by ntresch (9.4k points)
0 votes

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 (18k points)