2013-02-12 4 views
1

저는 Rails를 처음 사용합니다. 필자는 작업중인 프로젝트에 대해 사용자 계정을 작성하고 테스트를 실행하여 Rails 자습서를 탐색했습니다.이메일 형식으로 RSPEC이 실패했습니다

Failures: 

    1) when email format is invalid should be invalid 
    Failure/Error: @user.email = invalid_address 
    NoMethodError: 
     undefined method `email=' for nil:NilClass 
    # ./spec/models/user_spec.rb:71:in `block (3 levels) in <top (required)>' 
    # ./spec/models/user_spec.rb:70:in `each' 
    # ./spec/models/user_spec.rb:70:in `block (2 levels) in <top (required)>' 

    2) when email format is valid should be valid 
    Failure/Error: @user.email = valid_address 
    NoMethodError: 
     undefined method `email=' for nil:NilClass 
    # ./spec/models/user_spec.rb:81:in `block (3 levels) in <top (required)>' 
    # ./spec/models/user_spec.rb:80:in `each' 
    # ./spec/models/user_spec.rb:80:in `block (2 levels) in <top (required)>' 

Finished in 0.527 seconds 
9 examples, 2 failures 

Failed examples: 

rspec ./spec/models/user_spec.rb:67 # when email format is invalid should be invalid 
rspec ./spec/models/user_spec.rb:78 # when email format is valid should be valid 

실마리가 없습니다. 나는 그것이 무엇을 말하는가 보지만, 심지어 C & P 튜토리얼의 코드는 모든 것이 제대로되었는지 확인하기 위해 무엇을했는지 다시 확인합니다.

다음은 user_spec 파일입니다.

https://gist.github.com/pwz2k/4770845

다음은 user.rb 파일입니다.

https://gist.github.com/pwz2k/4770854

(가) I 이메일 유효성 검사를 추가 할 때까지 나타나지 않았다 실패합니다.

+0

nil : NilClass 오류는 @user가 존재하지 않거나 아무 것도 설정되어 있지 않음을 알려줍니다. 어떤 자습서를 사용하고 있습니까? –

+0

다음 부분적으로 michael hartl 자습서 http://ruby.railstutorial.org/book/ruby-on-rails-tutorial. 내 웹 사이트의 사용자 등록/프로필 부분을 만들려면이를 따라야했습니다. –

답변

0

테스트를 시작하기 전에 @user를 만들고 있는지 확인하십시오. 사용자 테스트에이 줄이 있습니까? (rails tutorial listing 6.16)

before do 
    @user = User.new(name: "Example User", email: "[email protected]") 
end 
+0

예. 'do do @user = User.new (이름 : "사용자 예", 이메일 : "[email protected]", 암호 : "foobar", password_confirmation : "foobar")' –

0

오류가 매우 분명합니다. 이메일을 nil 클래스로 설정합니다. 당신은 우리가 오류가없는 사용자가없는 불평하고, 사용자의 이메일 없을 것

@user.email = "something" 

말을 사용자에게 이메일을 설정하는데있어 어떤 의미합니다.

여기에 작업하려면이 문제를 해결하는 데 도움이되는 예제 코드를 사용하십시오.

describe "validations" do 
    before(:each) do 
    @user = User.new(name: "gates", email: "[email protected]") 
    end 
    it "should be invalid" do 
    invalid_emails = %w{[email protected] p.com name.gmail.com foo.ymail} 
    invalid_emails.each do |invalid_email| 
     @user.email = invalid_email 
     expect(@user.email).to_not be_valid 
    end 
    end 

    it "should be valid" do 
    @user.name = "somename" 
    valid_emails = %w{[email protected] [email protected] [email protected] 
    [email protected]} 
    valid_emails.each do |valid_email| 
    @user.email = valid_email 
    expect(@user.email).to be_valid 
    end 
    end 
end 

fixures 폴더에 사용자를위한 fixure를 만들었는지 확인하십시오.

희망이 도움이됩니다.

관련 문제