2011-10-30 41 views
2

我試圖模擬從通用的父類延遲的方法。知道我的代碼看起來像這樣。如何模擬繼承的方法

public interface IBaseRepository<T> 
{ 
    IEnumerable<T> FindMany(Func<T, bool> condition); 
} 

public interface IPersonRepository : IBaseRepository<person> 
{ 
    //Here I got some specifics methods for person repository 
} 

我的測試代碼看起來像這樣;

private IPersonRepository mockPersonRepository { get; set; } 

    [TestMethod] 
    public void TestMehtod() 
    { 
     LogonModel model = CreateLogonModel("[email protected]", "test", "Index"); 
     person p = new person() { Email = model.Email, password = model.Password, PersonId = 1 }; 

     mockPersonRepository.Stub(x => x.FindMany(y => y.Email == model.Email && y.password == model.Password)).Return(new List<person> {p}); 
     mockPersonRepository.Replay(); 

     var actual = instanceToTest.LogOnPosted(model) as PartialViewResult; 

     Assert.AreEqual("_Login", actual.ViewName); 
    } 

當我在vs 2010中使用調試工具時,我可以認爲我存根,沒有工作,返回的人總是空。我已經宣佈FindMany方法爲虛擬。

有人知道如何存根該方法嗎?我使用RhinoMocks。

回答

2

的問題是,你比較拉姆達 - 但你真正興趣在person實例傳遞到拉姆達基於滿足謂詞條件符合您person對象 - 您可以使用Matches()通過只是爲了實現這一目標執行上p謂詞 - 如果這相當於true比你有一場比賽,而應返回廢止列表:

mockPersonRepository.Stub(x => x.FindMany(Arg<Func<person, bool>>.Matches(y => y(p)))) 
        .Return(new List<person> { p }); 
+0

謝謝您的幫助,這真的幫了我!現在我想測試一下,我真的使用傳遞給方法LogonPosted的模型。我怎樣才能做到這一點?我一直試圖這樣做: mockPersonRepository.Expect(x => x.FindMany(Arg >。Matches(y => y(p))))。 但是,如果我設置了我的測試,它總是變綠。我如何期待Im真的用模型中的數據調用personRepository。 – Rikard