Question

I am trying to set public property of mocked object and the test fails to run. View is a public property of Presenter.

    [TestMethod]
    public void CustomerSearchPresenter_OnSearch_ValidateFromCustomerId_ExpectFalseWithInvalidNumber()
    {
        var customerSearchPresenter = new CustomerSearchPresenter();
        customerSearchPresenter.View = MockRepository.GenerateMock<ICustomerSearchView>();
        customerSearchPresenter.View.FromCustomerId = "test"; --> it is not working

        //Act
        customerSearchPresenter.OnSearch();

        //Assert
        Assert.IsFalse(customerSearchPresenter.View.IsValidFromCustomerId);
    }
Was it helpful?

Solution

I don't think you can set a property of the mocked view directly. Remember most mocking frameworks will generate a dynamic proxy for the ICustomerSearchView that you need.

This means that all field, property, method setup etc. needs to go through the mocking framework so that it can internally arrange the proxy for you. Finally the object exposed by the mocking framework will behave like the way you set it up.

So in this case, you want to setup a property of the view with a value such that when people access the property, you need a particular value to be returned.

Normally mocking frameworks have property setup code as follows:

// create the mock for the view.
var viewMock = new Mock<ICustomerSearchView>();

// setup the property value
viewMock.SetupGet(v => v.FromCustomerId).Returns("test");

customerSearchPresenter.View = viewMock.Object; // the actual View.

Try the same with your code..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top