0

我目前正在阅读 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

.Where 必须得到一个 bool 谓词,但你正在向它传递一个字符串。尝试将其更改为类似

 .Where(x => x.ContentRole == roleData.ContentRole);
4

3 回答 3

2

在阅读 Hartl 教程时,如果您遇到undefined local variable or method错误,或者想了解教程中的当前变量或方法的定义应该是什么,以下技术很有帮助:

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

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

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

于 2013-10-07T14:12:54.533 回答
2

This nolonger applies to the tutorial by Michael Hartl http://www.railstutorial.org/book

Spork is not being used in tutorial for Rails 4 now, this may change, I followed the page linked below but it will overwrite the existing spec setup and install Gems not required by the tutorial.

Are you using Spork? If so you have to restart it any time you make changes in routes.rb.

Described in Listing 3.38:

One word of advice when using Spork: after changing a file included in the prefork loading (such as routes.rb), you will have to restart the Spork server to load the new Rails environment. If your tests are failing when you think they should be passing, quit the Spork server with Ctrl-C and restart it.

http://ruby.railstutorial.org/chapters/static-pages#sec-spork

于 2013-11-08T04:19:42.473 回答
0

对于 Rails 5,将代码添加到 rails_helper.rb :
include Rails.application.routes.url_helpers

于 2017-05-02T13:00:40.843 回答