开发者

Unable to figure out rhino mocks issue

开发者 https://www.devze.com 2022-12-19 13:33 出处:网络
In a method in presenter,I expect a method of view to be called.This method is also passed data extracted from a service method(which is not mocked).This service method basically gets data from databa

In a method in presenter,I expect a method of view to be called.This method is also passed data extracted from a service method(which is not mocked).This service method basically gets data from database and 开发者_如何学Goreturns List(using LINQ to SQL).Now, when I write this in the test

 List<customers> cus = expecteddata;
 view.AssertWasCalled(v => v.InitializeCustomersForSelectedCity(cus));      

Rhino.Mocks.Exceptions.ExpectationViolationException:   ICustomerListView.InitializeCustomersForSelectedCity(System.Collections.Generic.List`1[DAL.Customer]); Expected #1, Actual #0.

The code which I am testing in presenter

  public void HandleSelectedCity(int City)
    {
        selectedCity = City ;
        _custometListForm.InitializeCustomersForSelectedCity(_CustomerListService.GetActiveCustomersForSelectedCity(selectedCity));            
    }

When I ignore arguments, test works fine What could be the issue?


You assertion creates an expectation based on cus, a variable defined in the unit test. However, when InitializeCustomersForSelectedCity is invoked, it's being invoked with the result of GetActiveCustomersForSelectedCity - a different instance of List<customers>.

Expectations setups basically perform an object.Equals operation on the expected instance and the actual instance. In your case, they are different, and the expectaction is not satisfied.

Either you need to loosen your expectation to accept any List<customers>, or you need to mock GetActiveCustomersForSelectedCity as well so that you can define the returned result from the unit test.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号