Ruby on rails 错误,失败/错误:预期{单击按钮提交}。不更改(用户,:计数)
我被困在迈克尔·哈特尔的RubyonRails教程的第7章 我的路线.rbRuby on rails 错误,失败/错误:预期{单击按钮提交}。不更改(用户,:计数),ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Rspec Rails,我被困在迈克尔·哈特尔的RubyonRails教程的第7章 我的路线.rb SampleApp::Application.routes.draw do get "users/new" resources :users root 'static_pages#home' match '/signup', to: 'users#new', via: 'get' match '/help', to: 'static_pages#help', vi
SampleApp::Application.routes.draw do
get "users/new"
resources :users
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
我的规格/请求/用户\u页面\u spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
let(:submit) { "Create my account" }
let(:submit) { "Sign up" }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
我的应用程序/views/users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
当我尝试运行测试时,我得到以下结果:
gvyntyk@gvyntyk-r60:~/rails_projects/sample_app$ bundle exec rspec spec/
...............FF......................
Failures:
1) User pages with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
NameError:
undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_3:0xad40120>
# ./spec/requests/user_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:27:in `block (3 levels) in <top (required)>'
2) User pages with valid information should create a user
Failure/Error: fill_in "Name", with: "Example User"
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:33:in `block (3 levels) in <top (required)>'
Finished in 1.54 seconds
39 examples, 2 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:26 # User pages with invalid information should not create a user
rspec ./spec/requests/user_pages_spec.rb:39 # User pages with valid information should create a user
有人能解释一下这项测试失败的原因吗?请确保你在这一章中的某一点上测试应该通过。有些测试在您更改代码后不应该通过。 另外,您的第二个错误似乎是身份验证问题。找不到字段名可能意味着在运行此测试时,用户应该已注册。我不太确定你发布的片段,但要确保你所有的缩进都是正确的,并且你的“末端”在正确的位置闭合。您现在拥有代码的方式看起来不像是在使用有效信息执行之前注册。尝试将描述注册页面do的结尾放在描述用户页面do块结尾之前的最底部。从单击按钮文档: 按id、文本、值或标题查找按钮并单击它 因此,您可以为单击按钮方法提供按钮值。而不是:
expect { click_button submit }.not_to change(User, :count)
应该是:
expect { click_button 'Create my account' }.not_to change(User, :count)
您将收到以下错误:
Failure/Error: fill_in "Name", with: "Example User"
Capybara::ElementNotFound:
Unable to find field "Name"
因为您忘了在以下两种情况下访问注册路径:
及
块。是否应该单击按钮:提交?对不起。。。什么意思?在expect{click_button:submit}上编辑expect{click_button:submit}?我的意思是您正在尝试执行未定义的提交方法,而您应该传递一个符号:submitSorry,但我不明白我错在哪里了。现在按钮提交创建我的帐户工作正常,但并没有通过测试。我认为它。。。在成功注册之前通过了测试。
describe "with invalid information" do
describe "with valid information" do