2011-11-16 98 views
6

水豚的自述文件(見Using Capybara with MiniTest::Spec)說我可以做到這一點,如果我正確地包括模塊,但它沒有給出任何說明性示例如何......我試過包括這樣的模塊:水豚驗收DSL與MiniTest ::規格?

class MiniTest::Spec 
    include Capybara::DSL 
end 

...無濟於事。我一直得到這個錯誤:

<main>': undefined method feature' for main:Object (NoMethodError)

我怎樣才能得到它的工作,因爲它寫在註釋掉的代碼?


規格/接受/ 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 

回答

2

有在this post一個很好的描述你應該如何讓MinitTest :: Spec運行於水豚。在那裏,他主要包括水豚:: DSL到基類的所有規格的中

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

這個工程很好,而在我們的設置,但當然不會重開MINITEST ::規格。

+0

是啊,我看到了帖子,但只允許你使用水豚的匹配,而不是BDD DSL進行驗收測試,這是什麼我之後... – neezer

+0

在[link](https://github.com/jnicklas/capybara)上,關於Capybara DSL的部分包含在「使用RSpec的Capybara」一節中,它說「最後,Capybara也來了用內置的DSL創建描述性驗收測試:「。然而,在展示DSL實際運行後,它說:「'feature'實際上只是'describe ...,:type =>:request'的別名,'background'是'before'和'scenario'的別名爲'它'。「所以如果你想在MiniTest中使用它,因爲它是RSpec唯一的功能,所以你運氣不好,但是你可能試着定義你自己的別名 – Patru

+0

鏈接的博客帖子已從Posterous中刪除,但是在這裏是在Internet Archive:http://web.archive.org/web/20130314201201/http://code-ningja.posterous.com/73460416 –

0

您應該添加minitest-rails-capybara寶石到Gemfile並添加文字「功能」來描述的末尾,如下所示:

 
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 

特殊詞「功能」是區分incensitive,和可以是「瀏覽器」。您可以通過添加一行到test_helper.rb定製:

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