2012-04-03 3 views
0

railstutorial.org의 7 장의 연습 문제 4에 문제가 있습니다.레일 튜토리얼 7 장, 연습 문제 4

다음
describe "signup" do 
    before { visit signup_path } 

    describe "with invalid information" do 
     it "should not create a user" do 
      expect { click_button "Create my Account".not_to change(User, :count) } 
     end 
    end 

    describe "error messages" do 
     before { click_button "Create my account" } 

     it { should have_selector('title', text: "Sign up") } 
     it { should have_content('error') } 
    end 

    describe "with valid information" do 
     before do 
      fill_in "Name", with: "Example User" 
      fill_in "Email", with: "[email protected]" 
      fill_in "Password", with: "foobar" 
      fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
      expect do 
       click_button "Create my account" 
      end.to change(User, :count).by(1) 
     end 
    end 

    describe "after saving the user" do 
     before { click_button "Create my account" } 
     let(:user) { User.find_by_email('[email protected]') } 

     it { should have_selector('title', text: user.name) } 
     it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
    end 
end 

는이 테스트 해야하는 것입니다, users_controller.rb :

<% provide(:title, @user.name) %> 
<div class="row"> 
<aside class="span4"> 
    <section> 
     <h1> 
      <%= gravatar_for @user %> 
      <%= @user.name %> 
     </h1> 
    </section> 
</aside> 
</div> 
:

def create 
@user = User.new(params[:user]) 
if @user.save 
    flash[:success] = "Welcome to the Sample App!" 
    redirect_to @user 
else 
    render 'new' 
end 
end 

여기뿐만 아니라 show.html.erb 코드의 다음

는 시험이다

테스트를 실행하면 다음과 같이 나타납니다.

$ bundle exec rspec spec/requests/user_pages_spec.rb 
........FF 

Failures: 

    1) User Pages signup after saving the user 
Failure/Error: it { should have_selector('title', text: user.name) } 
NoMethodError: 
    undefined method `name' for nil:NilClass 
# ./spec/requests/user_pages_spec.rb:57:in `block (4 levels) in <top (required)>' 

    2) User Pages signup after saving the user 
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
    expected css "div.alert.alert-success" with text "Welcome" to return something 
# ./spec/requests/user_pages_spec.rb:58:in `block (4 levels) in <top (required)>' 

Finished in 0.86152 seconds 
10 examples, 2 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:57 # User Pages signup after saving the user 
rspec ./spec/requests/user_pages_spec.rb:58 # User Pages signup after saving the user 

테스트 사용자에게 테스트 데이터베이스를 저장해야하지만, 어떤 이유로 user.name이 nil이됩니다. 어떤 아이디어?

감사합니다. 모두의 컨텍스트를 이해하기 위해 구체적으로 코드를 통해 이동하지 않고

답변

1

는, 그것이 usernil입니다입니다, 그 user.namenil을 반환 아니다, 따라서 여기에 본 어떠한 방법/부동산 name이 없습니다 :

undefined method `name' for nil:NilClass 

당신은 기호 :user 정의 테스트 케이스 전에 여기이 선이 있습니다

let(:user) { User.find_by_email('[email protected]') } 

아직 당신은 객체를 참조 테스트에 user :

it { should have_selector('title', text: user.name) } 

변경 이전에 user에있는 기호 :user과 테스트를 통과해야한다.

+0

답장을 보내 주셔서 감사합니다. 나는 그것을 알아. "사용자를 저장 한 후"do 블록이 "유효한 정보"블록의 범위를 벗어 났으므로 양식이 채워지지 않았습니다 (nil이 나오는 곳). 나는 "유효한 정보가있는"블록 안에 "사용자 블록을 저장 한 후"를 넣고 테스트가 통과되었습니다. 답장을 보내 주시면 답변을 수락 한 것으로 표시하겠습니다. :-) – dartfrog

+0

아, 그 대답에 쓰고 자신의 것을 받아 들여서 다른 사람이이 같은 문제에 걸려 넘어지면 그들이 어디로 가고 있는지 알 수 있습니다 :) – djlumley

+1

당신은 문제가 전혀 없다고 맞습니다. 'user'와 nil'user.name'이 아니라 제안 된 해결책이 옳지 않습니다. 'let'에서': user' 심볼을 사용하면'user'라는 지역 변수가 자동으로 생성됩니다. 자세한 내용은 [Rails 튜토리얼 샘플 앱 참조 구현] (https://github.com/railstutorial/sample_app_2nd_ed)을 참조하십시오. 'user @ example.com'이라는 전자 메일을 가진 사용자가 어떻게 든 'click_button'명령으로 생성되지 않는 것이 문제입니다. 왜 그런지는 모르겠지만 해결 방법으로'user.name'을''Example User ''로 바꾸면 이름을 하드 코딩 할 수 있습니다. – mhartl

관련 문제