ruby-on-rails - 如何在 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_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)

知道我做错了什么吗?

最佳答案

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

关于ruby-on-rails - 如何在 shoulda 宏中创建上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2708632/

相关文章:

Ruby: 'bundle exec' 为所有 shell 命令抛出错误

ruby-on-rails - 如何测试引发错误的方法的副作用

android - 使用 Ruby on Rails 进行应用程序开发

ruby-on-rails - 在 Rails ActiveRecord 迁移中, `using:` 关键字

ruby-on-rails - 你能通过 ruby​​ 脚本自动重命名 postgres 数据库吗?

javascript - Ruby json gem 正在编码 html 实体

ruby-on-rails - Rails gem acts_as_list : How to handle reordering of list items when a list item is destroyed?

ruby-on-rails - 您如何跟踪 View 的页面浏览量

c# - 自动化测试期间的 WPF 组件资源

angular - MockBackend 和 HttpTestingController 有什么区别?