【问题标题】:How to extend World in the context of cucumber-rails?如何在 cucumber-rails 的上下文中扩展 World?
【发布时间】:2018-09-16 01:18:40
【问题描述】:

我正在使用 rails-api gem 使用 Rails 构建 API。我想使用 cucumber-rails 和 gem 'Airborne' 来测试它。

Airborne 附带了一些很好的辅助方法来测试 API 响应,我希望在我的步骤定义中可以访问这些方法。我之前在 Sinatra 中做过这种事情,在 /features/env.rb 文件中配置比较简单。

然而,似乎有了 rails-cucumber,“世界”的创建发生在幕后某处,我不知道如何扩展它以在创建后使用 Airborne 模块。

我尝试了以下方法:

Airborne.configure do |config|
  config.rack_app = Rails.application
end

Cucumber::Rails::World.extend(Airborne)

When(/^I make a request for information about an event$/) do
  get "/events/1"
end

Then(/^I receive the information as a JSON$/) do
  expect_json {} 
end

我仍然在 #expect_json 上收到 NoMethodError,这是一种 Airborne 方法。

所以我的问题是:如何在 cucumber-rails 的上下文中扩展 World 的实例?

【问题讨论】:

    标签: ruby-on-rails cucumber


    【解决方案1】:

    不要惊慌,世界已经被拯救了。解决方案是将 Airborne 和其他任何东西包装在一个模块中:

    module MyHelpers
      include Airborne
      include Capybara::DSL
    end
    

    然后传递:

    World(MyHelpers)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多