我定义了以下路由并定义了以下 UsersController,当我尝试运行在 { visit signup_path } 之前尝试的规范测试时,我收到以下错误:
错误
Failure/Error: before { visit signup_path}
ActionController::RoutingError:
uninitialized constant UserController
# ./spec/requests/user_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
resources :users
路线.rb
resources :users
match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
root :to => 'static_pages#help'
users_controller.rb
class UsersController < ApplicationController
def new
end
def show
@user = User.find(params[:id])
end
end
更新 **user_pages_spec.rb**
describe "UserPages" do
subject { page }
describe "Signup Page" do
before { visit signup_path}
it "Should have header Sign-up" do
should have_selector("h1", text: "Sign-up")
should have_selector("title", text: "Sign-up")
end
end
describe "profile page" do
let(:user) {FactoryGirl.create(:user)}
before { visit user_path(user) }
it { should have_selector("title", text: "#{user.name} Profile")}
it { should have_selector("h1", text: user.name)}
end
end
这是我的路线:
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
signup /signup(.:format) users#new
help /help(.:format) static_pages#help
root / static_pages#help
我可以毫无问题地访问 www.blah.com/signup。但是规范正在寻找一个单一的用户控制器。我也可以用“signup_path”代替“/users/new”,测试就会通过。
另外,想补充一点,最初我使用单一版本的 rails 生成器创建了控制器(仅)。但是在我发现使用 rails destroy 控制器脚本后,我放弃了它。然后为复数版本发布了正确的 rails 生成器。就像我说的,它就像测试搞砸了,但实际的应用程序。