0

我一直在关注 Michael Hartl 的《Ruby on Rails 3 Tutorial》一书。在第 8 章的集成测试之前,它一直进展顺利。我运行以下命令来创建集成测试:

rails generate integration_test users

它创建了一个文件,然后我添加了 2 个测试;一个代表失败(空表单),一个代表成功(实际有效的表单字段)。

这是上面命令生成的 users_spec.rb 文件:

require 'spec_helper'
require 'database_cleaner'

describe "Users" do

  describe "GET /users" do
    it "works! (now write some real specs)" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get users_index_path
      response.status.should be(200)
    end
  end

  describe "signup" do

    describe "failure" do
      it "should not make a new user with empty data" do
        lambda do
          visit signup_path
          fill_in "Name",         :with => ""
          fill_in "Email",        :with => ""
          fill_in "Password",     :with => ""
          fill_in "Confirmation", :with => ""
          click_button
          response.should render_template('users/new')
          response.should have_selector("div#error_explanation")
        end.should_not change(User, :count)
      end #end "should not make a new user with empty data" do
    end #end describe failure

    describe "success" do
      it "should make a new user" do
        lambda do
          visit signup_path
          fill_in "Name",         :with => "Example User"
          fill_in "Email",        :with => "user@example.com"
          fill_in "Password",     :with => "foobar"
          fill_in "Confirmation", :with => "foobar"
          click_button
          response.should have_selector("div.flash.success", :content => "Welcome")
          response.should render_template('users/new')
        end.should change(User, :count).by(1)
      end
    end #end success
  end #end describe signup
end #end describe Users

我不知道为什么,但是所有的测试都在不应该的时候失败了。我错过了一些明显的东西吗?我已经运行了 rake db:migrate,清理了数据库,但我就是不明白。

控制台错误来自:rspec spec/requests/users_spec.rb

JorgeZapata:sample_app jorgezapata$ rspec spec/requests/users_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
FFF

Failures:

  1) Users GET /users works! (now write some real specs)
     Failure/Error: get users_index_path
     NameError:
       undefined local variable or method `users_index_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007ffd9bbd6550>
     # ./spec/requests/users_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) Users signup failure should not make a new user with empty data
     Failure/Error: visit signup_path
     ActionView::Template::Error:
       /Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'
       ....label :password_confirmation "Confirmation" );@output_buffe...
       ...                               ^
     # <internal:prelude>:10:in `synchronize'
     # ./spec/requests/users_spec.rb:18:in `block (5 levels) in <top (required)>'
     # ./spec/requests/users_spec.rb:17:in `block (4 levels) in <top (required)>'

  3) Users signup success should make a new user
     Failure/Error: visit signup_path
     ActionView::Template::Error:
       /Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'
       ....label :password_confirmation "Confirmation" );@output_buffe...
       ...                               ^
     # <internal:prelude>:10:in `synchronize'
     # ./spec/requests/users_spec.rb:33:in `block (5 levels) in <top (required)>'
     # ./spec/requests/users_spec.rb:32:in `block (4 levels) in <top (required)>'

Finished in 0.12243 seconds
3 examples, 3 failures

Failed examples:

rspec ./spec/requests/users_spec.rb:6 # Users GET /users works! (now write some real specs)
rspec ./spec/requests/users_spec.rb:16 # Users signup failure should not make a new user with empty data
rspec ./spec/requests/users_spec.rb:31 # Users signup success should make a new user

提前致谢,请指教!

4

1 回答 1

1

你在错误中有你的答案。

1:

undefined local variable or method `users_index_path'

路径不存在。从控制台运行rake routes以获取可用路线的列表。很有可能是users_path

2和3:

ActionView::Template::Error:
   /Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'

您的 app/views/users/new.html.erb 文件的第 17 行存在语法错误。

学习阅读错误输出。它会告诉您(通常)您需要知道的一切。

于 2013-08-15T04:03:34.600 回答