Search code examples
c#.netunit-testingrhino-mocksarrange-act-assert

How can I set the returned value of this Stub object?


public interface IMyINterface
{
    int GetMeSomeInteger();
    Toy GetMeAToy(string toyName);
}


[TestMethod]
public void PlayWithANumber_RecievesInteger_DoRightJob()
{

    IMyINterface stub = MockRepository.GenerateStub<IMyINterface>();

    // HOW CAN I? :
    // Instruct GetMeSomeIngeter() method in stub to return 5

    // HOW CAN I? :
    // Instruct GetMeAToy(string toyName) method in stub to return
      //new Toy() {ToyName = "Gizmo", Code = "0989"}

      var five = stub.GetMeSomeInteger();
      var gizmo = GetMeAToy("Gizmo");
      Assert.IsTrue(DoSomething(five, gizmo) == 100 );    
}

Solution

  • Scenario 1:

    var myInterface = MockRepository.GenerateStub<IMyINterface>();
    myInterface.Stub(x => x.GetMeSomeIngeter()).Return(5);
    

    Scenario 2:

    var myInterface = MockRepository.GenerateStub<IMyINterface>();
    myInterface.Stub(x => x.GetMeAToy("Gizmo")).Return(new Toy() {ToyName = "Gizmo", Code = "0989"});