This is a migrated thread and some comments may be shown as answers.

Assert Occurs not Working

1 Answer 74 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 11 Jun 2010, 08:43 PM
The following test in VS 2010:

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

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.

Joe

1 Answer, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 14 Jun 2010, 11:47 AM
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);   
ord.GetCustomerAddress();  
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.

Regards,
Mehfuz


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.
Tags
General Discussions
Asked by
Joe
Top achievements
Rank 2
Answers by
Ricky
Telerik team
Share this question
or