我被困在教程的第7.3节中,它说测试应该在最后通过,但是当我运行bundle exec rspec spec / requests / user_pages_spec.rb -e“注册无效信息”时它仍然会失败。
$ bundle exec rspec spec/requests/user_pages_spec.rb -e "signup with invalid information"
Run options: include {:full_description=>/signup\ with\ invalid\ information/}
F
Failures:
1) User pages signup with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
ActiveModel::ForbiddenAttributesError:
ActiveModel::ForbiddenAttributesError
# ./app/controllers/users_controller.rb:12:in `create'
# ./spec/requests/user_pages_spec.rb:28:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:28:in `block (4 levels) in <top (required)>'
Finished in 0.13176 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:27 # User pages signup with invalid information should not create a user
Randomized with seed 31885
这是我的/spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
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 "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
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
end
继承了users_controller.rb文件
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user]) # Not the final implementation!
if @user.save
# Handle a successful save.
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
和我的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>
有什么想法吗?