2011-10-07 52 views
2
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);  
} 

回答

4

方案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"}); 
+0

但我並不需要一個模擬;我需要一個存根。另外,你認爲我們可以在不使用Record的情況下處理這個問題;我的意思是通過Arrange-Assert-Act方法? – pencilCake

+0

我只描述了安排步驟..您還需要調用您的成員(這將是Act),然後檢查返回的值(將會是Assert)。 – Samich

+0

我只剩下最新的樣品。 – Samich