Rhino Mock Test Expected #1, Actual #0 - error

1.4k views Asked by At

I'm newbie working with Rhino Mock and I'm getting this error that I cannot understand why. Here the test

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

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

I want to test GetStudiesByInstallationReference was called one time.

What am I doing wrong?...it should pass the test as the Expect clause is the same used in the Stub but I still got the exception

Expected #1, Actual #0.

Anybody could help me with this?

Thanks in advance

1

There are 1 answers

1
Matt Cole On

I want to test GetStudiesByInstallationReference was called one time.

GetStudiesByInstallationReference is a type, and not a method that you expect to be called.

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

This line from your code is setting up an expectation on the repository mock. It expects that the Query() method is called with a parameter of type GetStudiesByInstallationReference that has the correct installation reference GUID as a property. If this method isn't called with the correct parameter, you will get the error you describe when calling repository.VerifyAllExpectations().

It looks like your test is missing the actual call to the SUT i.e. the "Act" in Arrange/Act/Assert. Simply put, you need to execute some code that will cause the method on your repository to be called as you expect (or change the test).