我更新了Rspec,现在我的一半Rspec测试都失败了。
最初,所有的测试都失败了,我可以通过添加" config.infer_spec_type_from_file_location来获得其中一半的测试!"到spec_helper.rb。
第一种类型的错误消息是参数错误:
1) todo_lists/new renders new todo_list form
Failure/Error: assigns(:todo_list, stub_model(TodoList,
ArgumentError:
wrong number of arguments (2 for 0..1)
# ./spec/views/todo_lists/new.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'
第二种类型是Capybara :: ElementNotFound:
5) Editing Todo Lists displays error when description is empty
Failure/Error: fill_in "Title", with: options[:title]
Capybara::ElementNotFound:
Unable to find field "Title"
# ./spec/features/todo_lists/edit_spec.rb:18:in `update_todo_lists'
# ./spec/features/todo_lists/edit_spec.rb:55:in `block (2 levels) in <top (required)>'
我的Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.1.4'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'capybara'
gem 'shoulda-matchers'
end
gem 'rspec-activemodel-mocks'
Spec_Helper文件:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include TodoListHelpers, type: :feature
config.include Capybara::DSL
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
config.order = "random"
end
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
todo_lists / new.html.erb_spec:
require 'spec_helper'
describe "todo_lists/new" do
before(:each) do
assigns(:todo_list, stub_model(TodoList,
:title => "MyString",
:description => "MyText"
).as_new_record)
end
it "renders new todo_list form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form[action=?][method=?]", todo_lists_path, "post" do
assert_select "input#todo_list_title[name=?]", "todo_list[title]"
assert_select "textarea#todo_list_description[name=?]", "todo_list[description]"
end
end
end
特征/ todo_lists / edit_spec.rb
require 'spec_helper'
describe "Editing Todo Lists" do
let!(:todo_list) {TodoList.create(title: "Groceries",
description: "Grocery list")}
def update_todo_lists(options={})
options[:title] ||= "New Title"
options[:description] ||= "New Description"
todo_list = options[:todo_list]
visit '/todo_lists'
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
fill_in "Title", with: options[:title]
fill_in "Description", with: options[:description]
click_button "Update Todo list"
end
it "displays error when description is empty" do
update_todo_lists todo_list: todo_list, description: ""
description = todo_list.description
todo_list.reload
expect(page).to have_content("error")
expect(todo_list.description).to eq(description)
end
编辑视图:
<h1>Editing todo_list</h1>
|
和表单助手:
<%= form_for(@todo_list) do |f| %>
<% if @todo_list.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todo_list.errors.count, "error") %>
`prohibited this todo_list from being saved:</h2>`
<ul>
<% @todo_list.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:0)
在您的视图文件中,您将text_field设为:title
,但在规范文件中,您正在填写标题。
Capybara无法访问。这就是为什么Capybara没有找到Title。
其他解决方案可能是尝试在控制台中检查您的标题。标题的ID是什么。
如果id类似于&#34; todo_title&#34;那么你应该在spec文件中保持不变。
喜欢这个fill_in "todo_title"
而不是fill_in "Title"
您可以使用上述任何解决方案解决此问题。它对我有用。
您也可以参考this。