【问题标题】:Rhino Mock Test Expected #1, Actual #0 - errorRhino Mock Test 预期 #1,实际 #0 - 错误
【发布时间】:2016-12-30 14:51:55
【问题描述】:

我是使用 Rhino Mock 的新手,我遇到了这个错误,我不明白为什么。这里是测试

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。

有人可以帮我吗?

提前致谢

【问题讨论】:

  • 您的代码示例不包含repository 的类型,因此不清楚该对象是什么。请注意,您只能在接口或具体类的virtual/abstract 成员上设置期望值(即:使用Expect()Stub())。

标签: c# unit-testing rhino-mocks rhino expected-exception


【解决方案1】:

我想测试一次调用 GetStudiesByInstallationReference。

GetStudiesByInstallationReference 是一种类型,而不是您希望调用的方法。

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

您的代码中的这一行正在对 repository 模拟设置期望。它期望使用GetStudiesByInstallationReference 类型的参数调用Query() 方法,该参数具有正确的安装引用GUID 作为属性。如果没有用正确的参数调用这个方法,你会得到你在调用repository.VerifyAllExpectations()时描述的错误。

您的测试似乎缺少对SUT 的实际调用,即Arrange/Act/Assert 中的“Act”。简而言之,您需要执行一些代码,以使您的存储库上的方法按您的预期调用(或更改测试)。

【讨论】:

  • 感谢您的回答。我会和一位同事核对你所说的内容(我创建这个测试时他不在场)......他曾经使用 Rhino,并且可能会帮助我。有趣的是,当我更改Expect(在Stub 之前)的顺序时......它起作用了。不知道有没有道理...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多