ruby-on-rails - Rails 3 和 Cucumber - 创建关联的步骤定义

标签 ruby-on-rails ruby-on-rails-3 cucumber capybara

我有两个 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

我希望找到或创建给定技术对象的名称(名称是技术对象的参数)并创建关联,以便注释属于该技术。

然后,最后一步:

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

最佳答案

快速回答:

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

关于ruby-on-rails - Rails 3 和 Cucumber - 创建关联的步骤定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486929/

相关文章:

cucumber - 行为驱动开发中的 "feature"和 "story"有什么区别?

ruby-on-rails - 查询近似相等

ruby-on-rails - Rails 模型和 ActiveRecord 中的子类化

html - 使用 iPhone Safari 的 YouTube iframe 页面上的链接和按钮响应较慢

ruby-on-rails - HABTM重复记录

javascript - 语法错误 : unterminated string literal

ruby-on-rails - rails : Combine multiple tables search conditions

ruby-on-rails-3 - 如何确定模型/嵌套模型中的任何字段是否发生更改?

ruby - 如何在 cucumber.feature 测试中嵌入 ruby​​ 变量

ruby-on-rails - Cucumber场景下的if语句怎么做?