【发布时间】:2015-02-07 00:47:50
【问题描述】:
我卡在 Michael Hartl 的 Ruby on Rails 教程的第 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', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
我的规范/请求/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
我的应用程序/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
谁能解释一下为什么这个测试失败了?
【问题讨论】:
-
不应该是
click_button :submit吗? -
打扰一下……什么意思?在期望 { click_button :submit } 上编辑期望 { click_button submit }?
-
我的意思是你正在尝试执行未定义的
submit方法,而你应该传递一个符号:submit -
抱歉,我不明白我在哪里弄错了。现在按钮提交(“创建我的帐户”)正常工作,但未通过测试。
标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails