Ruby on rails 如何在shoulda宏中创建上下文
使用较小的代码示例再次询问此问题:Ruby on rails 如何在shoulda宏中创建上下文,ruby-on-rails,ruby,testing,macros,shoulda,Ruby On Rails,Ruby,Testing,Macros,Shoulda,使用较小的代码示例再次询问此问题: # this is a dummy shoulda macro that creates a context def self.macro_context context "macro" do yield end end # i am expecting this test to fail within the macro context context "some context" do macro_
# this is a dummy shoulda macro that creates a context
def self.macro_context
context "macro" do
yield
end
end
# i am expecting this test to fail within the macro context
context "some context" do
macro_context do
should "test" do
fail
end
end
end
所以我希望看到的是:
1) Error:
test: some context macro context should test. (TestClassName)
1) Error:
test: some context should test. (TestClassName)
但我得到的只是:
所以我希望看到的是:
1) Error:
test: some context macro context should test. (TestClassName)
1) Error:
test: some context should test. (TestClassName)
知道我做错了什么吗?我的代码中有类似的东西。我这样做是为了
test/shoulda\u macros/whatever\u file.rb
def self.should_require_login(actions = [:index], &block)
if (actions.is_a? Symbol)
actions = [actions]
end
context "without user" do
actions.each do |action|
should "redirect #{action.to_s} away" do
get action
assert_redirected_to login_path
end
end
end
if block_given?
context "active user logged in" do
setup do
@user = Factory.create(:user)
@user.register!
@user.activate!
login_as(@user)
end
merge_block(&block)
end
end
end
感谢Francisco的代码,要解决这个问题,您不能只在新上下文中生成块,您必须使用shoulda的方法。然后应该是这样的:
def self.macro_context(&block)
context "macro" do
merge_block(&block)
end
end
唯一的区别似乎是在merge_block调用中,我以前没有看到过,它做什么?正如它的名字所说,它会将您传递的块与您调用的块合并
merge_block
。在您的情况下,它将在上下文“活动用户登录”中合并给定的块。。。结束