Set Property Expect setter in Moq

I have a piece of code like this, accept user login, if failed, set error message. simple enough, but when using Moq, I couldn’t figure out how to set the expectation of the returning error message at first.


        [Test]
        public void CanStopInValidLogin()
        {

            var mockedView = new Mock<ILoginView>();
            mockedView.Expect(x => x.UserName).Returns("invalid_user");
            mockedView.Expect(x => x.Password).Returns("invalid_password");
            string errmsg = "";

            mockedView.ExpectSet(x => x.ErrorMessage)
                .Callback( v => errmsg = v);

            _presenter = new LoginPresenter(mockedView.Object) { WindowsLoginMode = false };
            _presenter.DoLogin();

            // Can't use mockedView.Object to get property, because it's virtual(?)
            //Assert.AreEqual(LoginPresenter.LOGIN_FAILURE, mockedView.Object.ErrorMessage);

            // Should use callback to set the internal passing value to a outsider!?
            Assert.AreEqual(LoginPresenter.LOGIN_FAILURE, errmsg);

            Assert.IsEmpty( Csla.ApplicationContext.User.Identity.Name);
            Assert.IsEmpty(((AglcPrincipal)Csla.ApplicationContext.User).Identity.Roles);

        }

Not sure if this is the only way to make it, Moq seems very weird to mock properties setter.

Advertisements

3 thoughts on “Set Property Expect setter in Moq

  1. What you’re looking for is stub behavior on that property. That is, for it to automatically save the value and be able to retrieve it for the assertions.

    That’s in Moq.Contrib, which gives you the following extension method:

    var mock = new Mock();
    // start “tracking” sets/gets to this property
    mock.Stub(v => v.Value);
    // provide a default value for the stub’ed property
    mock.Stub(v => v.Value, 5);

    Now you can do:

    IHaveValue v = mock.Object;
    // Initial value was stored
    Assert.Equal(5, v.Value);

    // New value set which changes the initial value
    v.Value = 6;
    Assert.Equal(6, v.Value);

  2. Thanks Daniel, this is very helpful.

    My revised code looks like this:

            [Test]
            public void CanStopInValidLogin()
            {
                var mockedView = new Mock<ILoginView>();
    
                mockedView.Stub(x => x.UserName, "invalid_user");
                mockedView.Stub(x => x.Password, "invalid_password");
                mockedView.Stub(x => x.ErrorMessage);
    
                _presenter = new LoginPresenter(mockedView.Object) { WindowsLoginMode = false };
                _presenter.DoLogin();
    
                Assert.AreEqual(LoginPresenter.LOGIN_FAILURE, mockedView.Object.ErrorMessage);
                Assert.IsEmpty( Csla.ApplicationContext.User.Identity.Name);
                Assert.IsEmpty(((AglcPrincipal)Csla.ApplicationContext.User).Identity.Roles);
     
    
            }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s