2012-04-03 27 views
0

railstutorial.orgの第7章の演習4に問題があります。ここでRailsチュートリアル第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、テストすることになっているものである:

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コードも同様です:

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

テストを実行すると、次のようになります。

$ 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

すべてのコンテキストを理解するために詳細にあなたのコードの上に行くがなければ、それはuser.namenilを返していることはありません、それはusernilあるので、ここで見られるように、何のメソッド/プロパティ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

関連する問題