11

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.

onof
  • 17,167
  • 7
  • 49
  • 85

2 Answers2

27
public interface IFred
{
    string UserName { set; }
}

[Test]
public void TestMethod1()
{
    IFred fred = MockRepository.GenerateMock<IFred>();
    fred.UserName = "Jim";
    fred.AssertWasCalled(x => x.UserName = "Jim");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

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
  • 39,709
  • 13
  • 100
  • 125
  • FYI, this is using MsTests so you may have to adjust your attributes accordingly – bendewey Feb 08 '09 at 20:22
  • 1
    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. –  Feb 08 '09 at 20:32
  • So do I get the approved answer? – bendewey Feb 08 '09 at 20:41
  • Of course you do - my first question - still learning about site - will be much quicker next time! –  Feb 09 '09 at 07:03