如何设置这个Stub对象的返回值?

本文关键字:对象 Stub 返回值 何设置 设置 | 更新日期: 2023-09-27 18:10:34

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 );    
}

如何设置这个Stub对象的返回值?

场景1:

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

场景2:

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