我正在尝试运行此测试,但由于某种原因它失败了。
describe "User Pages" do
subject { page }
.
.
describe "Sign up" do
before { visit signup_path }
let(:submit) { "Sign Me Up" }
.
.
describe "with valid information" do
before do
fill_in "name", with: "newuser"
fill_in "Username", with: "user254"
fill_in "Email", with: "user@yahoo.com"
select "Male", from: "gender"
fill_in "Password", with: "password"
fill_in "Confirm Password", with: "password"
end
it "Should create a user" do
expect { click_button submit}.to change(User, :count).by(1)
end
end
然而,当我运行测试时,我收到此错误
Failures:
1) User Pages signup with valid information Should create a user
Failure/Error: expect { click_button submit}.to change(User, :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/requests/user_pages_spec.rb:35:in `block (4 levels) in <top (required)>'
Finished in 4.61 seconds
35 examples, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:34 # User Pages signup with valid information Should create a user
这是我在我的控制器中的创建动作
def create
@user = User.new(params[:user])
if @user.save
redirect_to @user
else
render 'new'
end
end
当我尝试在浏览器中注册时,注册成功。我的测试有什么问题吗?感谢
答案 0 :(得分:1)
检查用户是否正在保存在控制器上:
def create
@user = User.new(params[:user])
puts "valid? = #{@user.valid?.inspect}"
if @user.save
puts 'saved!'
redirect_to @user
else
puts 'not saved'
puts @user.errors.inspect
render 'new'
end
end
将打印控制器进程,您将知道发生了什么
如果没有打印出来,那么问题是一些before_filter阻止了创建操作被调用
答案 1 :(得分:0)
尝试
it "Should create a user" do
click_button submit
User.count.should == 1
end