Question

I am trying to test the below method using RhinoMocks and MbUnit however I am unable to get the test to pass. The current error is when the expect call for "" is not found.

The function is in vb.net and the test is in c#

 Public Function Login(user As Website.BusinessObjects.User) As Integer Implements IActivityLog.Login

        Dim item As BOAudit.IActivityLog = New BOAudit.ActivityLog(_request)

        ' Activity
        item.UserID = User.GuidID
        item.Type = Enums.ActivityType.Login
        item.Description = String.Format(If(IsAdmin, "Logged in as {0}", "Logged in"), User.FullName)
        item.EventUserID = _authenticatedUser.GuidID

        Return _dalActivityLog.Save(item)

    End Function

The test below is what I currently have and I believe the issue is down to declaring a new object within the function above and not passing that object into the function. What is the best way to test the above function and should I be passing in the object?

[Test]
    public void Login_Data_NewRecordCreated()
    {
        const int id = 99;
        var data = new Website.CodeModules.BusinessObjects.Audit.ActivityLog(_request)
        {
            Type = Enums.ActivityType.Login,
            Description = "Logged in",
            EventUserID = _user.GuidID
        };

        var user = _mocks.StrictMock<User>();

        using (_mocks.Record())
        {
            Expect.Call(_dalActivityLog.Save(data)).Return(id);
        }

        using (_mocks.Playback())
        {
            var result = _balActivityLog.Login(user);

            Assert.AreEqual(id, result);
        }

    }
Was it helpful?

Solution

The condition you assert in your test does not seem to have much sense. Your code seems to test that the mock instance in _dalActivityLog returns the constant that you've set up.

In that test you should test the code of the function Login, not the _dalActivityLog implementation. Thus, you should check that _dalActivityLog.Save is called with the right parameter passed.

I suppose that _dalActivityLog is an instance of a class that implements an interface that you haven't specified in your question. Let's call it IActivityLog. Then you should set up a mock instance of it in your test code.

IActivityLog logMock = MockRepository.GenerateStub<IActivityLog>();

Then you inject somehow this mock instance into the instance of the class the has the Login method (via constructor or property).

Then call your Login method and pass there an instance of User.

Then you make the assertion about the _dalActivityLog.Save call, like the one below.

logMock.AssertWasCalled(
  call => call.Save(
    Arg<Website.CodeModules.BusinessObjects.Audit.ActivityLog>.Matches(
      logItem => logItem.UserID == user.GuidID && logItem.Type == Enums.ActivityType.Login
    )
  )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top