Ruby on Rails 教程(3.2) 第 7 章 练习 清单 7.32 rspec 问题



添加清单 7.32 中提供的 rspec 测试后,rspec 在使用 find_by_email 方法返回 User 对象时遇到问题。rspec 测试失败,并显示消息">NoMethodError:nil:NilClass 的未定义方法名称",这实际上是告诉我它找不到用户,因此没有返回 User 对象。任何关于为什么这种特定的查找器方法在其他方法工作时失败的见解都会有所帮助。

我使用第 7 章示例部分开头提供的信息添加了一个用户,该部分指定了">Rails 教程"作为名称,">example@railstutorial.org"作为电子邮件。我还更改了 rspec have_selector 方法,以匹配 HTML h1标签中的用户名而不是 HTML 标题标签。如果我使用其他查找器方法(如 User.first 或 User.find(1(,则 rspec 测试通过,但在使用 User.find_by_email("example@railstutorial.org"( 时失败。如果我打开 rails 控制台(开发模式(,我可以使用 find_by_email 方法返回正确的用户。如果我打开 rails 控制台(测试模式(,数据库中没有用户。我运行了bundle exec rake db:test:prepare并重新运行了它,但 rspec 测试仍然失败。当更改规范以使用 User.first 并打印出带有 pp user.name, user.email 的名称和电子邮件时,我可以打印出正确的结果,但在尝试使用 User.find_by_email("example@railstutorial.org"( 时 rspec 会丢失。

环境

Ruby 1.9, rails 3.2.3, Ubuntu 10.04

宝石文件

source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'bootstrap-sass','2.0.0'
gem 'bcrypt-ruby','3.0.1'
group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails','2.9.0'
  gem 'annotate', '~> 2.4.1.beta'
end
group :test do
  gem 'capybara','1.1.2'
  gem 'factory_girl_rails','1.4.0'
end
#Javascript runtime for Linux
gem 'therubyracer' 
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'

用户模型

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password
  before_save { self.email.downcase! }
  validates :name, presence: true, length: {maximum:20}
  VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  validates :email, presence: true, 
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false}
  validates :password, length: {minimum:6}
  validates :password_confirmation, presence: true
end

user_pages rspec(片段(

describe "signup" do
  before { visit signup_path }
  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
    describe "after submission" do
      before { click_button submit }
      it { should have_selector('title',text: 'Sign up') }
      it { should have_content('error') } 
    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
    describe "after saving the user" do
      before { click_button submit }
      let(:user) { User.find_by_email("example@railstutorial.org") }
      it { should have_selector('h1', text: user.name) #error with name on NilClass!
      it { should have_selector('div.alert.alert-success', text: 'Welcome')}
    end
  end
end

您用 email= user@example.com 填写表单,但尝试使用 email= example@railstutorial.org 在数据库中查找用户。使它们相同。

最新更新