如何在 shoulda 宏中创建上下文

发布于 2024-08-30 08:25:43 字数 616 浏览 4 评论 0原文

用较小的代码示例再次问这个问题:

  # 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)

知道我做错了什么吗?

Asking this question again with smaller code sample:

  # 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

So what I would expect is to see:

  1) Error:
  test: some context macro context should test. (TestClassName)

But I am getting only this:

So what I would expect is to see:

  1) Error:
  test: some context should test. (TestClassName)

Any idea what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-09-06 08:25:43

我的代码中有类似的东西。我在 test/shoulda_macros/whatever_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

I have something similar in my code. And I did it like this into test/shoulda_macros/whatever_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
浅紫色的梦幻 2024-09-06 08:25:43

感谢弗朗西斯科提供的代码,要解决此问题,您不能只在新上下文中生成块,您必须使用 shoulda 的 merge_block 方法。那么它应该看起来像这样:

  def self.macro_context(&block)
    context "macro" do
      merge_block(&block)
    end
  end

Thanks Francisco for the code, to fix this you cannot just yield the block inside of your new context, you have to use shoulda's merge_block method. It then should look like this:

  def self.macro_context(&block)
    context "macro" do
      merge_block(&block)
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文