开发者

unit test with lambda fail using rhino mock

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.CreateM开发者_如何学JAVAember(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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜