如何分解常见的“before(:each)”调用 RSpec 以便多个规范可以使用它们?
我想分解这堆代码,以便我的所有控制器测试(嗯,几乎所有)都使用这个 before(:each)
块:
before(:each) do
@user = User.new
controller.stub(:authenticate_user!)
controller.stub(:current_user).and_return(@user)
controller.stub(:add_secure_model_data)
end
有什么方法可以做到这一点吗?我不想将它包含在所有控制器中......因为有一些控制器不需要它。基本上,从 SecureController
扩展的每个控制器都需要这个 before(:each)
块。
有什么好的方法可以做到这一点吗?
谢谢
I'd like to factor this bunch of code so that all of my controller tests (well, almost all of them) use this before(:each)
block:
before(:each) do
@user = User.new
controller.stub(:authenticate_user!)
controller.stub(:current_user).and_return(@user)
controller.stub(:add_secure_model_data)
end
Is there any way to do that? I don't want to include it in all controllers... because there are a few that don't need this. By basically, every controller that extends from SecureController
will need this before(:each)
block.
Is there any nice way to do that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://relishapp.com/rspec/rspec-core/dir/example-groups/共享上下文
http://relishapp.com/rspec/rspec-core/dir/example-groups/shared-context
因此,将该块放入
SecureController。
如果
SecureController
的特定子级不需要此功能,您可以创建另一个中间超类,或者使用有条件你掌控。So put the block into
SecureController.
If there are specific children of
SecureController
that don't want this functionality, you could make another intermediate superclass, or wrap the method call with a conditional you control.