0

If I have this test

Expect.Call(_session.Single<Admin>(x => x.Email == userModel.Email)).Repeat.Once().Return(null);

Telling me

Rhino.Mocks.Exceptions.ExpectationViolationException : ISession.Single(x => (x.Email == value(Enquete.Test.Controllers.MemberControllerTest+<>c__DisplayClassb).userModel.Email)); Expected #1, Actual #0.

It fails but if I add .IgnoreArguments() it works. Is it possible to test using lambda? If I debug I can see that my Email is the same.

Here's the full test :

[Test]
        public void Register_Post_ReturnRedirectOnSuccess()
        {
            _builder.InitializeController(_controller);

            var userModel = TestHelper.CreateMemberModel();
            var returnMemberRole = "Member";
            var tempPassword = "Val1dPass";
            var member = TestHelper.CreateMember(userModel);
            var emailSubscription = "subscription@corpiq.com";
            var subjectNotification = "sujet du meessaaggee";
            var mailUseSSL = "true";
            var message = userModel.FirstName + " " + userModel.LastName + " s'est inscrit au système d'enquête en ligne, veuillez confirmer son inscription.";
            member.PasswordExpire = DateTime.Now.AddDays(-1);
            member.Phone = userModel.Phone;
            member.MemberNumber = userModel.MemberNumber;
            member.PasswordExpire = DateTime.Now.AddDays(-1);

            Expect.Call(_session.Single<Admin>(x => x.Email == userModel.Email)).Repeat.Once().Return(null);
            Expect.Call(_session.Single<Member>(x => x.Email == userModel.Email)).Repeat.Once().IgnoreArguments().Return(null);
            Expect.Call(_authService.GeneratePassword()).Repeat.Once().Return(tempPassword);
            Expect.Call(_authService.MemberRole).Repeat.Once().Return(returnMemberRole);
            Expect.Call(_authService.RegisterUser(userModel.Email, tempPassword, returnMemberRole)).Repeat.Once().Return(MembershipCreateStatus.Success);
            _session.Add(member);
            LastCall.Repeat.Once();
            _session.CommitChanges();
            LastCall.Repeat.Once();
            Expect.Call(_configHelper.GetValue("emailSubscription")).Repeat.Once().Return(emailSubscription);
            Expect.Call(_configHelper.GetValue("subjectNotification")).Repeat.Once().Return(subjectNotification);
            Expect.Call(_configHelper.GetValue("MailUseSSL")).Repeat.Once().Return(mailUseSSL);
            _mailHelper.SendMail(emailSubscription, subjectNotification, message, Convert.ToBoolean(mailUseSSL));
            LastCall.Repeat.Once();

            _mock.ReplayAll();
            var result = _controller.Register(userModel);

            _mock.VerifyAll();
            result.AssertActionRedirect().ToAction<MemberController>(c => c.RegisterSuccess());

        }

Thank you!

VinnyG
  • 6,883
  • 7
  • 58
  • 76
  • Whenever I see anyone using Rhino and having trouble, I suggest you check out Moq by Daniel Cazullino (and others). It may not solve your problem, but it helped clean up my tests and forced me to learn lambdas better. – Jason Slocomb Dec 02 '10 at 22:37

2 Answers2

1

The lambda in your unit test compiles into a class-level method (a method inside your unit test). Inside your controller, a different lambda compiles into a class-level method (inside the controller). Two different methods are used so Rhino Mocks shows the expectation error. More here: http://groups.google.com/group/rhinomocks/browse_frm/thread/a33b165c16fc48ee?tvc=1

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • So do you think the IgnoreArgument option is a good approach? Do you have an other solution? – VinnyG Dec 02 '10 at 22:53
  • I would verify that data is set properly after you call your controller's method instead of setting expectations on specific lambdas -- which, as you see, are difficult to check for equality. – PatrickSteele Dec 03 '10 at 03:56
1

Unfortunately lambdas in C# use reference equality, not value equality. Try the following:

Func<bool> f1 = () => true;
Func<bool> f2 = () => true;
Console.WriteLine("f1 == f2 results in " + (f1 == f2));

Guess what? The answer is False.

It's also false for Expression...

Expression<Func<bool>> f1 = () => true;
Expression<Func<bool>> f2 = () => true;
Console.WriteLine(f1.ToString());          // Outputs "() => True"
Console.WriteLine("a1 == a2 results in " + (f1 == f2)); // False

Unfortunately the best way to solve this (and its ugly) in C# (and therefore Rhino Mocks) is to use ToString() on Expressions and compare those.

Expression<Func<bool>> f1 = () => true;
Expression<Func<bool>> f2 = () => true;
Console.WriteLine(f1.ToString());        // Outputs "() => True"
Console.WriteLine("a1 == a2 results in " + (f1.ToString() == f2.ToString()));  // True

You have to be careful with this technique as two Expressions, "x => x" and "y => y", although equivalent functionally, will still evaluate to false due to the different parameters. Also be aware that you must do this with Expression and not Func or Action for this ToString() trick to work.

James Kovacs
  • 11,549
  • 40
  • 44