ruby-on-rails - 失败/错误:在 { visit signup_path } 之前 NameError:#<RSpec::Core::ExampleGroup 的未定义局部变量或方法 `signup_path'

标签 ruby-on-rails testing rspec railstutorial.org

我目前正在学习 Rails 教程的第七章,似乎停留在图 7.22 处。简而言之,我无法让测试通过。当我跑的时候。 . .

bundle exec rspec spec/requests/user_pages_spec.rb

. . .我看到一堆失败的测试,上面写着:

 Failures:

1) User pages signup page 
    Failure/Error: before { visit signup_path }
    NameError:
    undefined local variable or method `signup_path' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb2be028410>
 # ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

2) User pages signup page 
   Failure/Error: before { visit signup_path }
   NameError:
   undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb2be048ad0>
 # ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

3) User pages signup page with invalid information should not create a user
   Failure/Error: before { visit signup_path }
   NameError:
   undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fb2be062e80>
 # ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

4) User pages signup page with valid information should create a user
   Failure/Error: before { visit signup_path }
   NameError:
   undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_2:0x007fb2be083158>
 # ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

Finished in 0.00374 seconds
4 examples, 4 failures

Failed examples:

rspec ./user_pages_spec.rb:11 # User pages signup page 
rspec ./user_pages_spec.rb:12 # User pages signup page 
rspec ./user_pages_spec.rb:18 # User pages signup page with invalid information should not create a user
rspec ./user_pages_spec.rb:32 # User pages signup page with valid information should create a user

Randomized with seed 10291

我猜主要错误涉及未定义的方法或变量“signup_path”,但我不知道应该在哪里定义它,或者它是否应该在某个时候由 Rails 自动定义。

有人可以帮我吗?

更新:下面是我的路线文件。

感谢您的回复。这是我的路线文件。除非我遗漏了什么,否则一切对我来说都很好:

SecondSampleApp::Application.routes.draw do

  get "users/new"
  get "static_pages/home"
  get "static_pages/help"
  get "static_pages/about"

  resources :users

  root "static_pages#home"

  match "/signup",  to: "users#new",            via: "get"
  match "/help",    to: "static_pages#help",    via: "get"
  match "/about",   to: "static_pages#about",   via: "get"
  match "/contact", to: "static_pages#contact", via: "get"


end

规范/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'


Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }


ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|


config.include Capybara::DSL

config.fixture_path = "#{::Rails.root}/spec/fixtures"

config.use_transactional_fixtures = true

config.infer_base_class_for_anonymous_controllers = false

config.order = "random"

config.include Rails.application.routes.url_helpers
end

这是 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')) }

    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

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

end

最佳答案

在阅读 Hartl 教程时,遇到 undefined local variable or method 错误,或者想找出变量或方法的当前定义在教程,以下技术很有帮助:

  • 转到网上的书
  • 点击右上角的“查看单个页面”链接
  • 使用浏览器的搜索命令,从您在教程中的位置向后搜索有问题的变量/方法名称。 (注意:如果它们很多,并且您确定它是一种方法,则可以将搜索范围缩小到“def variable_or_method_in_question”)
  • 记下定义和定义所在的文件(通常在表/列表/图的标题中给出)

访问 github 存储库有时也很有帮助,但它显示的是代码的最终状态/位置,这通常会产生误导。

在您的情况下,此技术会导致在 Table 5.1 中找到一个条目这表示此变量是 Rails 根据 routes.rb 文件的内容生成的路径。您应该检查该文件的内容,并在命令行上使用 rake routes 查看当前定义的路由。

关于ruby-on-rails - 失败/错误:在 { visit signup_path } 之前 NameError:#<RSpec::Core::ExampleGroup 的未定义局部变量或方法 `signup_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217985/

相关文章:

ruby-on-rails - 总是使用 response_to?

ruby-on-rails - 命名空间类的 FactoryGirl 工厂

ruby-on-rails - Rspec expect( ) 与 expect { }

r - 明确设计用于测试 Web Scraping 应用程序的网站

javascript - 等待用户操作

xcode - 是否可以在 XCode 中使用两台 iPad 测试我的应用程序?

ruby - Test First 的温度对象项目 - 它通过了所有测试,但这就是我应该学习的内容吗?

ruby-on-rails - Ruby Object#id 警告和 Active Record

ruby-on-rails - 如何在RoR 中限制用户访问页面?

ruby-on-rails - Rails 设计自定义 url