2

私は 2 つの Rails リソースを持っています: メモと技術です。

注モデル:

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

これで、指定された Technology オブジェクトの名前 (名前は Technology オブジェクトのパラメーター) を find_or_create し、メモがテクノロジに属するように関連付けを作成します。

次に、最後のステップ:

And I should see "JavaScript"

Note#index の各ノート インスタンスの横に note.technology.name が表示されていることを確認します。

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

この Cucumber 機能を実行すると、ステップは最後まで通過し、「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
4

1 に答える 1

2

素早い回答:

@note.technology = Technology.find_or_create_by_name(tech)
@note.save
于 2012-07-14T20:14:32.473 に答える