新手问题 RSpec 里面提示 undefined local variable or method `signup_path'

gechentuo · 2013年09月03日 · 最后由 bage 回复于 2014年06月04日 · 3793 次阅读

我的项目中测试环境为:

group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'capybara', '2.1.0'
end

rake routes:

     Prefix Verb   URI Pattern               Controller#Action
      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
            PATCH  /users/:id(.:format)      users#update
            PUT    /users/:id(.:format)      users#update
            DELETE /users/:id(.:format)      users#destroy
   sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
    session DELETE /sessions/:id(.:format)   sessions#destroy
       root GET    /                         static_pages#home
       help GET    /help(.:format)           static_pages#help
      about GET    /about(.:format)          static_pages#about
    contact GET    /contact(.:format)        static_pages#contact
     signup GET    /signup(.:format)         users#new
     signin GET    /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy

测试文件:static_pages_spec.rb

require 'spec_helper'

describe "StaticPages" do

 describe "Home Page" do

  it "should have the content 'Sample App'" do
   visit signup_path
    expect(page).to have_content('Sample App')
  end
end

end 

运行:rspec static_pages_spec.rb 报错:

Failures:

  1) StaticPages Home Page should have the content 'Sample App'
     Failure/Error: visit signup_path
     NameError:
       undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a3a4>
     # ./static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.00196 seconds
1 example, 1 failure

Failed examples:

rspec ./static_pages_spec.rb:7 # StaticPages Home Page should have the content 'Sample App'

Randomized with seed 47907

spec_helper.rb 里没有载入 rails ? 参考 test/test_helper.rb 的设置改改

建议楼主上传一个头像...

楼主怎么解决这个问题的?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号