views:

977

answers:

2

If you have a property:

public class Fred
{
   public string UserName
   {
     set
     {
        userName=value;
     }
   }
}

how do you use Rhino Mocks to check that

fred= new Fred();
fred.UserName="Jim";

is called.

Expect.Call(mockFred.UserName).SetPropertyWithArgument("Jim");

does not compile.

+1  A: 

You should just be able to do a verify all on the property set

[TestClass]
public class FredTests
{
    [TestMethod]
    public void TestFred()
    {
        var mocker = new MockRepository();
        var fredMock = mocker.DynamicMock<IFred>();

        fredMock.UserName = "Name";
        // the last call is actually to the set method of username
        LastCall.IgnoreArguments(); 
        mocker.ReplayAll();

        fredMock.UserName = "Some Test that does this.";
        mocker.VerifyAll();
    }

}

public interface IFred
{
    string UserName { set; }
}
bendewey
FYI, this is using MsTests so you may have to adjust your attributes accordingly
bendewey
Thank you - for reasons that are totally beyond me, when I tried what you suggested it didn't work. It does now. Silly me - thank you.
So do I get the approved answer?
bendewey
Of course you do - my first question - still learning about site - will be much quicker next time!
+7  A: 
public interface IFred
{
    string UserName { set; }
}

[Test]
public void TestMethod1()
{
    IFred fred = MockRepository.GenerateStub<IFred>();
    fred.UserName = "Jim";
    fred.AssertWasCalled(x => x.UserName = "Jim");
}
Darin Dimitrov