Assert Occurs not Working

2 posts, 1 answers
  1. Joe
    Joe avatar
    12 posts
    Member since:
    Apr 2007

    Posted 11 Jun 2010 Link to this post

    The following test in VS 2010:

                ICustomer cust = Mock.Create<ICustomer>();  
                Order ord = new Order(cust);  
                Mock.Assert(() => cust.GetAddress(), Occurs.Once());  

    Where Customer is this:

    namespace MockTest  
        public class Customer : MockTest.ICustomer  
            public string GetAddress()  
                return "some address";  

    And Order is this:

    namespace MockTest  
        public class Order  
            ICustomer m_cust;  
            public Order(ICustomer cust)  
                m_cust = cust;  
            public string GetCustomerAddress()  
                return m_cust.GetAddress();  

    Results in a MockAssertionException with the following message:

    Exepcted call on the mock should be once.But was 0 times.

    As you can see in the GetCustomerAddress implementation of Order, the code does indeed call GetAddress on the ICustomer instance.  Additionally, message formatting and spelling is incorrect.

  2. Answer
    Ricky avatar
    467 posts

    Posted 14 Jun 2010 Link to this post

    Hi Joseph,
    In Typical AAA syntax , you first have to act then do assert. It  also ensures the original flow of control.  In your sample you are first Asserting then you are acting on it which is wrong.

    A typical brain flow could be  "I want to assert if the things goes as expected"

    The correct code will be :

    ICustomer cust = Mock.Create<ICustomer>();   
    Order ord = new Order(cust);   
    Mock.Assert(() => cust.GetAddress(), Occurs.Once());

    Thanks for the formating and typo, i will fix and let you know. Optionally, i can send you the update both for your previous one and this one via email, if you want.


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top