Capybara接受DSL与MiniTest :: Spec?

时间:2011-11-16 04:36:12

标签: ruby-on-rails-3 capybara dsl minitest

Capybara的自述文件(参见Using Capybara with MiniTest::Spec)说如果我正确地包含模块,我可以这样做,但它没有给出任何说明性的例子......我试过把模块包括在内这样:

class MiniTest::Spec
  include Capybara::DSL
end

......无济于事。我一直收到这个错误:

  对于main:Object(NoMethodError)

<main>': undefined method feature'

如何在注释掉的代码中编写它?


规格/接受/ API / reward_terms_spec.rb:

require "#{Dir.pwd}/spec/acceptance/acceptance_helper"

# this syntax works...

describe 'reward terms acceptance test' do
  include Capybara::DSL

  describe '#index' do
    specify {
      visit '/reward_terms'
      # ...
    }
  end
end

# this syntax doesn't work...

# feature 'RewardTerms', %q{
#   In order to get all reward terms available to me
#   As an API client
#   I want to list all active RewardTerms
# } do

#   background do
#     set_api_headers
#   end

#   scenario 'RewardTerm index' do
#     visit '/reward_terms'
#     ...
#   end
# end

规格/接受/ acceptance_helper.rb:

ENV["RAILS_ENV"] = "test"
require "#{Dir.pwd}/config/environment"

require 'minitest/autorun'
require 'capybara/rails'

def set_api_headers(device_id = 'abcd1234')
  header 'Accept', 'application/json'
  header 'X-Device-Id', device_id
end

3 个答案:

答案 0 :(得分:2)

this post中有一个很好的描述,你应该如何使用capybara运行MinitTest :: Spec。在那里,他基本上将Capybara :: DSL包含在所有规格的基类中,如

class RequestSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
end

这在我们的设置中相当不错,但当然它不会重新打开MiniTest :: Spec。

答案 1 :(得分:0)

这是一个简单的test_helper装备来运行功能和使用spec语法在Rails中进行集成测试。基于tenderlove的要点,article mentioned above re: MiniTest with Capybara,以及大量的修补和放大源波令。

https://gist.github.com/1607879

答案 2 :(得分:0)

您应该将minitest-rails-capybara gem添加到Gemfile并添加一个单词&#34; feature&#34;到描述的末尾如下:

feature 'RewardTerms feature', %q{
   In order to get all reward terms available to me
   As an API client
   I want to list all active RewardTerms
} do

  background do
    set_api_headers
  end

  scenario 'RewardTerm index' do
    visit '/reward_terms'
    #...
  end
end

特殊字&#34;功能&#34;是不敏感的,可以是#34;浏览器&#34;。您可以通过在test_helper.rb添加一行来自定义它:

MiniTest::Spec.register_spec_type(/FooBar\z/i, Capybara::Rails::TestCase)