我目前正在关注 Michael Hartl 的 Rails 教程,并且我已经成功升级到 7.22,没有遇到任何重大问题。但是,我对测试的输出感到困惑,它说:
Failures:
1) UserPages signup with invalid information should not create a user
Failure/Error: expect{click_button submit }.not_to change(User, :count)
AbstractController::ActionNotFound:
The action 'create' could not be found for UsersController
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:29:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:29:in `block (4 levels) in <top (required)>'
2) UserPages signup with valid information should create a user
Failure/Error: expect{click_button submit}.to change(User, :count).by(1)
AbstractController::ActionNotFound:
The action 'create' could not be found for UsersController
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:42:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:42:in `block (4 levels) in <top (required)>'
Finished in 0.7718 seconds
6 examples, 2 failures
按照教程的说明,我已将以下内容添加到我的用户控制器页面:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
end
但它似乎仍然不起作用。我试过添加一个创建方法,但这只会抛出一个丢失的模板错误......
如果它有帮助,这里是 rake routes 命令的输出:
~/dev/rails/sample_app$ rake routes
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root / static_pages#home
signup /signup(.:format) users#new
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
作为对评论的回应,失败的测试是:
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
提前感谢您的任何建议!