0

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

Elmer Dantas
  • 4,649
  • 5
  • 30
  • 36
  • Your code sample doesn't include the type of `repository`, so it's not clear what that object is. Note that you can only set expectations (ie: use `Expect()` or `Stub()`) on Interfaces or `virtual`/`abstract` members of concrete classes. – John M. Wright Jan 05 '17 at 14:32

1 Answers1

0

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).

Matt Cole
  • 2,491
  • 17
  • 21
  • Thanks for answering it. I'll check what you said with a colleague (whom wasn't here when I was creating this test)...he's used to use Rhino and probably will help me with it. The funny thing is, when I change the order of `Expect` (before the `Stub`) ...it works. I don't know if it's makes sense... – Elmer Dantas Jan 02 '17 at 08:05