2012-07-14 52 views
2

我有兩個rails資源:註釋&技術。Rails 3&Cucumber - 創建關聯的步驟定義

注型號:

class Note < ActiveRecord::Base 
    belongs_to :technology 
end 

支持下的湖南省型號:

class Technology < ActiveRecord::Base 
    has_many :notes 
end 

我想用下面的水豚步驟:

Scenario: Viewing notes and their technology association 
    Given there are the following notes: 
     | subject    | 
     | recursive functions | 
    And the note "recursive functions" has the technology "JavaScript" 
    And I am on the homepage 
    Then I should see "recursive functions" 
    And I should see "JavaScript" 

我用直線的步驟:

And the note "recursive functions" has the technology "JavaScript" 

是(我懷疑的問題是在以下):

Given /^the note "(.*?)" has the technology "(.*?)"$/ do |note, tech| 
    @note = Note.find_by_subject!(note) 
    @note.technology = Technology.find_or_create_by_name(tech) 
end 

我想這find_or_create給定的技術對象的姓名(名稱爲Technology對象的一個​​參數)並創建關聯,以便筆記屬於該技術。

然後,最後一步:

And I should see "JavaScript" 

是驗證旁邊的每一個音符例如在筆記#指數

<% @notes.each do |note| %> 
    <li> 
     <%= link_to note.subject, note %> 
     <% if note.technology %> 
      | Tech: <%= note.technology.name %>   
     <% end %> 
    </li> 
    <% end %> 

是note.technology.name顯示的是當我運行這個黃瓜功能直到最後一步,我應該看到「JavaScript」,錯誤:

expect there to be content "JavaScript" in "Note Index Page" 
recursive functions \n \n recursive functions \n \n (RSpec:Expectations::ExpectactionNotMetError 
./features/step_definitions/web_steps.rb:107 in '/^(?:|I)should see "([^"]*)"$/' 
features\viewing_notes.feature:20:in 'And I should see "JavsScript"' 

我知道該協會正在工作,因爲與表單創建這種關聯可行(並在Note#index上顯示@ note.technology.name)。這個問題似乎與我的步驟定義。有沒有更好的方法來測試這個來看看發生了什麼?也許寫一個Rspec規範?感謝您花時間提供幫助。

(可能相關的:)

gem 'rails', '3.0.0' 

group :test do 
    gem 'rspec-rails', '2.0' 
    gem 'factory_girl', '2.6.4' 
end 

group :cucumber do 
    gem 'cucumber-rails', '1.0.6' 
    gem 'capybara' 
end 

回答

2

快速回答:

@note.technology = Technology.find_or_create_by_name(tech) 
@note.save 
+0

好極了!我知道這可能是這樣的小事。謝謝。 – Drew 2012-07-14 20:30:12

相關問題