本文介绍了拉姆达与单元测试失败使用犀牛模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我有这样的测试 Expect.Call(_session.Single<管理>(X => x.Email == userModel.Email))Repeat.Once()返回(空)。 告诉我 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 = "[email protected]"; 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 这篇关于拉姆达与单元测试失败使用犀牛模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 02:04