如何在使用Rails Mailer参数化时如何最好地使用RSPEC模拟
我目前正在更改我们的栏杆邮件,以使用使用参数化的邮件器的较新方法,这使我们的代码基础与 rails指南,但更重要的是,它还允许在appsignal(例如appsignal)的日志和第三方应用程序中适当过滤参数。
IE。我正在将其更改
UserMailer.new_user_email(user).deliver_later
为
UserMailer.with(user: user).new_user_email.deliver_later
,但是我们有很多规格使用RSPEC模拟来确认使用适当的参数调用邮件器。通常,这些控制器实际上要求邮件器正确地花费电子邮件。
我们通常有类似的东西:
expect(UserMailer).to receive(:new_user_email)
.with(user)
.and_return(OpenStruct.new(deliver_later: true))
.once
但是现在,随着邮件的参数化,我看不到使用RSPEC模拟的任何简单方法来验证正确的邮件方法是否使用正确的参数调用。有人对现在如何最好地测试有什么想法吗?期望的可读性可能是这里的最大因素,理想情况下,它是一行,没有多行嘲笑设置。
注意:我真的不想实际运行邮件器,我们有测试实际邮件器正在工作的邮件单元规格。
I'm currently changing our rails mailers to use the newer way of using the mailer that uses parameterization, which brings our code base inline with the rails guide, but more importantly it also allows the parameters to be filtered appropriately in the logs and 3rd party apps like AppSignal.
ie. I'm changing this
UserMailer.new_user_email(user).deliver_later
to
UserMailer.with(user: user).new_user_email.deliver_later
But we have a quite a few specs that use Rspec Mocks to confirm that a mailer was called with the appropriate params. Generally these test that a controller actually asked the mailer to spend the email correctly.
We generally have something like:
expect(UserMailer).to receive(:new_user_email)
.with(user)
.and_return(OpenStruct.new(deliver_later: true))
.once
But now with the parameterization of the mailer, I don't see any easy way to use rspec mocks to verify that the correct mailer method was called with the correct params. Does anyone have any ideas on how best to test this now? Readability of the expectation is probably the biggest factor here, ideally it is one line without multiple lines of mocking setup.
Note: that I don't really want to actually run the mailer, we have mailer unit specs that test the actual mailer is working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,对于将来出现这个问题的其他任何人来说。我最终在规格/支持目录中添加了一个辅助方法,
然后用类似的内容可以在类似的规范中调用
,或者用于evelry_now,
它可以适应我们的情况,但是您可能需要调整它并为此添加零件如果您需要配置曾经限制的电子邮件或其他内容。
So for anyone else that hits this problem in the future. I ended up adding a helper method in the specs/support directory with something like this
Which can then be called in a spec like this
or for deliver_now
It works good enough for our situation, but you might need to adapt it and add a part for the amount of emails or something if you need to configure the once restriction.
当您有几个被链接的方法时,您可以使用
receed_message_chain
但是有一个反抽奖 - 它不支持
两次
的整个计数器的整个流利接口,因此您必须在这里做一个技巧:
When you have couple of methods which are chained you can use
receive_message_chain
But there is one backdraw - it doesn't support the whole fluent interface of counters like
once
twice
So you have to do one trick here: