0

リスト 7.32で提供されている rspec テストを追加した後、rspecはfind_by_emailメソッドを使用して User オブジェクトを返すのに問題があります。rspec テストは「NoMethodError: undefined method name for nil:NilClass」というメッセージで失敗します。これは基本的に、ユーザーが見つからなかったため、ユーザー オブジェクトが返されなかったことを示しています。この特定のファインダーメソッドが失敗している理由についての洞察は役に立ちます。

第 7 章の「例」セクションの冒頭で提供された情報を使用して、名前に「 Rails Tutorial 」、電子メールに「 example@railstutorial.org」を指定して、1 人のユーザーを追加しました。また、rspec のhave_selectorメソッドを変更して、HTML のタイトルタグではなく、HTMLのh1タグのユーザーと一致するようにしました。User.firstUser.find(1)などの他のファインダー メソッドを使用すると、rspec テストはパスしますが、 User.find_by_email("example@railstutorial.org")を使用すると失敗します。. Rails コンソール (開発モード) を開くと、find_by_email メソッドを使用して正しいユーザーを返すことができます。Railsコンソール(テストモード)を開くと、データベースにユーザーがいません。実行bundle exec rake db:test:prepareして再実行しましたが、rspec テストはまだ失敗しました。User.firstを使用するように仕様を変更し、名前電子メールを出力すると正しい結果を出力できますが、 User.find_by_email("example@railstutorial.org")pp user.name, user.emailを使用しようとすると rspec が失われます。

環境

ルビー 1.9、レール 3.2.3、Ubuntu 10.04

Gemfile

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-z\d\-.]+\.[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
4

1 に答える 1

1

フォームに email=user@example.comを入力しますが、データベースで email= を使用してユーザーを見つけようとしますexample@railstutorial.org。同じになるようにしてください。

于 2012-05-23T19:45:40.283 に答える