1

私はこれらの失敗を経験していて、問題を解決するためにどこを見ればよいのかわかりません。このサイトを検索しましたが、問題のある他のサイトを見つけることができません。Hartlチュートリアルによると、すべてのテストに合格する必要があります。

失敗:

  C:\Sites\rails_projects\sample_app>bundle exec rspec spec
Rack::File headers parameter replaces cache_control after Rack 1.5.
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m
←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m

Failures:

  1) when email address is already taken
     ←[31mFailure/Error:←[0m ←[31muser_with_same_email = @user.dup←[0m
     ←[31mTypeError:←[0m
       ←[31mcan't dup NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:104:in `dup'←[0m
←[36m     # ./spec/models/user_spec.rb:104:in `block (2 levels) in <top (require
d)>'←[0m

  2) when name is too long
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = "a" * 51 }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `name=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:76:in `block (2 levels) in <top (required
)>'←[0m

  3) when password is not present
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.password = @user.password_confi
rmation = " " }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:55:in `block (2 levels) in <top (required
)>'←[0m

  4) when password confirmation is nil
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.password_confirmation = nil }←[
0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:65:in `block (2 levels) in <top (required
)>'←[0m

  5) when email format is valid should be valid
     ←[31mFailure/Error:←[0m ←[31m@user.email = valid_address←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `email=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:97:in `block (3 levels) in <top (required
)>'←[0m
←[36m     # ./spec/models/user_spec.rb:96:in `each'←[0m
←[36m     # ./spec/models/user_spec.rb:96:in `block (2 levels) in <top (required
)>'←[0m

  6) when password doesn't match confirmation
     ←[31mFailure/Error:←[0m ←[31mbefore {@user.password_confirmation = "mismatc
h" }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required
)>'←[0m

  7) when name is not present
     ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = " " }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `name=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:71:in `block (2 levels) in <top (required
)>'←[0m

  8) when email format is invalid should be invalid
     ←[31mFailure/Error:←[0m ←[31m@user.email = invalid_address←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `email=' for nil:NilClass←[0m
←[36m     # ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required
)>'←[0m
←[36m     # ./spec/models/user_spec.rb:86:in `each'←[0m
←[36m     # ./spec/models/user_spec.rb:86:in `block (2 levels) in <top (required
)>'←[0m

Finished in 2.42 seconds
←[31m30 examples, 8 failures←[0m

Failed examples:

←[31mrspec ./spec/models/user_spec.rb:109←[0m ←[36m# when email address is alrea
dy taken ←[0m
←[31mrspec ./spec/models/user_spec.rb:77←[0m ←[36m# when name is too long ←[0m
←[31mrspec ./spec/models/user_spec.rb:56←[0m ←[36m# when password is not present
 ←[0m
←[31mrspec ./spec/models/user_spec.rb:66←[0m ←[36m# when password confirmation i
s nil ←[0m
←[31mrspec ./spec/models/user_spec.rb:94←[0m ←[36m# when email format is valid s
hould be valid←[0m
←[31mrspec ./spec/models/user_spec.rb:61←[0m ←[36m# when password doesn't match
confirmation ←[0m
←[31mrspec ./spec/models/user_spec.rb:72←[0m ←[36m# when name is not present ←[0
m
←[31mrspec ./spec/models/user_spec.rb:82←[0m ←[36m# when email format is invalid
 should be invalid←[0m

私のuser_spec.rbファイル:

    require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com",
      password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

  describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

  describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end
end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it {should_not be_valid}
  end

  describe "when password doesn't match confirmation" do
    before {@user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end


  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid } 
   end

  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end

    describe "when email format is invalid" do

    it "should be invalid" do

      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end      
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end      
    end
  end
  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

        it { should_not be_valid}
    end
  end

User.rbファイル:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
  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, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

助言がありますか?

4

2 に答える 2

1

おそらく、2つの余分な秒を追加しdescribe Userた後、誤ってブロックを閉じたように見えた可能性があります。describe "with invalid password"end

現在:

describe User do

  # ...
  describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end
end

# ...

end

する必要があります:

describe User do

  # ...
  describe "with invalid password" do
    let(:user_for_invalid_password) { found_user.authenticate("invalid") }

    it { should_not == user_for_invalid_password }
    specify { user_for_invalid_password.should be_false }    
  end

  # ...
end

endまた、ファイルの終わりに向かってインデントとsの数を確認してください。

于 2013-03-09T14:48:24.493 に答える
0

エラーメッセージは、@userがインスタンス化されていないことを示しています

:eachあなたが後を逃しているように見えますbefore

before do
    @user = User.new(name: "Example User", email: "user@example.com",
      password: "foobar", password_confirmation: "foobar")
end

する必要があります:

before (:each) do
    @user = User.new(name: "Example User", email: "user@example.com",
      password: "foobar", password_confirmation: "foobar")
end

@userオブジェクトが作成され、テストが緑色になります:)

于 2013-03-08T23:36:36.430 に答える