我有两个 Rails 资源:笔记和技术。
备注型号:
class Note < ActiveRecord::Base
belongs_to :technology
end
技术模型:
class Technology < ActiveRecord::Base
has_many :notes
end
我想使用以下 Capybara 步骤:
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
我希望它找到_or_create 给定技术对象的名称(名称是技术对象的参数)并创建关联,以便注释属于技术。
然后,最后一步:
And I should see "JavaScript"
是为了验证 note.technology.name 是否显示在 Note#index 上的每个笔记实例旁边
<% @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