我在迈克尔·哈特尔的Ruby on Rails教程中遇到了第7章。
我的routes.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', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
我的规范/ requests / user_pages_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
我的app / 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
有人可以解释为什么这个测试失败了吗?
答案 0 :(得分:0)
请确保您处于测试通过章节的某一点。更改代码后,有些测试不应该通过。 此外,您的第二个错误似乎是身份验证问题。无法找到字段&#34;名称&#34;可能意味着在此测试运行时,用户应该有sign_up。我对你发布的片段不太确定,但要确保你所有的缩进都是正确的,并且你的结尾是'#39;正在关闭正确的地方。你现在拥有代码的方式看起来并不像你在使用有效信息&#34;执行&#34;之前注册了。尝试放置&#34;结束&#34;描述&#34;注册页面&#34;在&#34;结束&#34;之前的最底层做&#34;描述&#34;用户页面&#34;做&#34;块。
答案 1 :(得分:0)
来自click_button
文档:
按ID,文字,值或标题查找按钮,然后点击它。
因此,您可以为click_button
方法提供按钮值。而不是:
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"
因为您忘记了visit signup_path
:
describe "with invalid information" do
和
describe "with valid information" do
块。