2016-12-30 78 views
0

我是新手與Rhino Mock工作,我得到這個錯誤,我不明白爲什麼。這裏測試犀牛模擬測試預期#1,實際#0 - 錯誤

public void TestGet() 
{ 
    var installationReference = new Guid("21D7D135-6E9E-4F92-8313-873CA3ABDCD8"); 
    var study = MockRepository.GenerateMock<IStudy>(); 
    var installation = MockRepository.GenerateMock<IInstallation>(); 
    var license = MockRepository.GenerateMock<ILicense>(); 
    var participant = MockRepository.GenerateMock<IStudyParticipant>(); 
    var clinicalPartner = MockRepository.GenerateMock<IClinicalPartner>(); 

    clinicalPartner.Stub(c => c.FirstName).Return("John"); 
    clinicalPartner.Stub(c => c.LastName).Return("Doe"); 
    installation.Stub(i => i.Reference).Return(installationReference); 
    license.Stub(l => l.Installations).Return(new List<IInstallation> { installation }); 
    participant.Stub(p => p.Licenses).Return(new List<ILicense> { license }); 
    participant.Stub(p => p.ClinicalPartner).Return(clinicalPartner); 
    participant.Stub(p => p.ClinicalPartnerStatus).Return(ClinicalPartnerStatus.Active); 

    study.Stub(s => s.Description).Return("Test WebAPI"); 
    study.Stub(s => s.Participants).Return(new List<IStudyParticipant> { participant }); 

    repository.Stub(r => r.Query(Arg<GetStudiesByInstallationReference>.Matches(s => s.InstallationReference.Equals(installationReference)))) 
     .Return(new DummyResult<IStudy>(study)); 
    repository.Expect(r => r.Query(Arg<GetStudiesByInstallationReference>.Matches(s => s.InstallationReference.Equals(installationReference)))).Return(new DummyResult<IStudy>(study)).Repeat.Once(); 
    repository.VerifyAllExpectations(); 
} 

GetStudiesByInstallationReference.cs

public class GetStudiesByInstallationReference : IQuery<IStudy> 
{ 
    public Guid InstallationReference { get; set; } 

    public GetStudiesByInstallationReference(Guid installationReference) 
    { 
     InstallationReference = installationReference; 
    } 

    public IQueryResult<IStudy> Execute(ISession session) 
    { 
     var criteria = session.CreateCriteria<IStudy>(); 
     criteria.CreateAlias("participants", "p"); 
     criteria.CreateAlias("p.licenses", "l"); 
     criteria.CreateAlias("l.installations", "i"); 
     criteria.Add(Restrictions.Eq("i.Reference", InstallationReference)); 
     criteria.Add(Restrictions.Eq("Status", StudyStatus.Approved)); 
     criteria.Add(Restrictions.Eq("p.ClinicalPartnerStatus", ClinicalPartnerStatus.Active)); 
     criteria.Add(Restrictions.Le("StartDate", DateTime.Now)); 
     criteria.Add(Restrictions.Or(
      Restrictions.IsNull("EndDate"), 
      Restrictions.Gt("EndDate", DateTime.Now))); 

     return new CriteriaResult<IStudy>(criteria); 
    } 
} 

我想測試GetStudiesByInstallationReference被稱爲一次。

我在做什麼錯了?...它應該通過測試爲Expect條款是在Stub使用的相同,但我仍然有異常

預計#1,實際0#。

有人可以幫助我嗎?

在此先感謝

+0

你的代碼示例不包括'repository'的類型,所以它不清楚該對象是什麼。請注意,您只能在接口或具體類的'virtual' /'abstract'成員上設置期望值(即:使用'Expect()'或'Stub()')。 –

回答

0

我想測試GetStudiesByInstallationReference被稱爲一次。

GetStudiesByInstallationReference是一種類型,而不是您期望被調用的方法。

repository 
    .Expect(r => r.Query(Arg<GetStudiesByInstallationReference>.Matches(s => s.InstallationReference.Equals(installationReference)))) 
    .Return(new DummyResult<IStudy>(study)).Repeat.Once(); 

這條線從你的代碼是建立在repository模擬的期望。它期望Query()方法通過類型爲GetStudiesByInstallationReference的參數調用,該參數具有正確的安裝引用GUID作爲屬性。如果此方法沒有使用正確的參數調用,則在致電repository.VerifyAllExpectations()時,會出現您描述的錯誤。

看起來您的測試看起來像是SUT的實際呼叫,即Arrange/Act/Assert中的「Act」。簡而言之,您需要執行一些代碼,以便按照您的預期調用存儲庫中的方法(或更改測試)。

+0

感謝您的回答。我會檢查你和一個同事說的話(當我創建這個測試的時候,他不在這裏)......他習慣使用Rhino,可能會幫助我。有趣的是,當我改變'Expect'的順序時(在'Stub'之前)......它可以工作。我不知道這是否有意義... –