【发布时间】:2015-01-03 21:35:14
【问题描述】:
我在我的 rails 应用程序中使用 RSpec v3.1.0 和 Capybara v2.3.0。我的 spec/features/user_creates_todo_spec.rb 中有以下内容。
require 'rails_helper'
feature 'User creates todo' do
scenario 'successfully' do
visit root_path
click_on 'Add a new todo'
fill_in 'Title', with: 'Buy milk'
click_on 'Submit'
expect(page).to have_css '.todos li', text: 'Buy milk'
end
end
然后在我的 app/views/todos/index.html.erb 中,我添加了以下内容:
<%= link_to "Add a new todo", new_todo_path %>
当我运行 rspec spec/features/user_creates_todo_spec.rb 时,测试失败并出现错误 Capybara::ElementNotFound: Unable to find link or button "Add a new todo"强>
但是,我预计错误是:未定义的局部变量或方法'new_todo_path',因为我已经在我的视图中定义了链接。奇怪的是,当我运行 rails 服务器并从浏览器导航到根路径时,我看到了未定义的局部变量或方法“new_todo_path”错误。此外,当我从根运行 rake 时,我看到了未定义的路径错误我的应用程序。那么,为什么当我使用 rspec 单独运行规范文件时,即使我已将链接添加到页面,我也会看到不同的错误?
谢谢
【问题讨论】:
标签: ruby-on-rails rspec tdd bdd rspec3