2011-07-14 5 views
1

初めてRoRで遊んでいて、奇妙なエラーが発生しました。シンボル参照を使用しているときにフィールドが見つからない

describe "when logged in" do 
    before(:each) do 
    @user = Factory(:user) 
    visit login_path 

    #the line below is the weird one 
    fill_in "session[user_name]", :with => @user.user_name 

    fill_in :password, :with => @user.password 
    click_button 
    end 

    it "should have navigation links" do 
    response.should have_selector("nav") 
    end 

    it "should have link to log out" do 
    response.should have_selector("a", :href => logout_path, :content => "Log out")  
    end 
end 

コード上でうまく動作しますが、私は

fill_in :user_name, :with => @user.user_name 

にライン

fill_in "session[user_name]", :with => @user.user_name 

を変更するならば、それは勝った」:私は私のナビゲーションリンクについて次のテストコードを持っていますなぜ仕事をするのか分かりません。あなたは私がパスワードを正確にそれを行う参照コードを見れば

<label for="session_user_name">User name</label><br/> 
<input id="session_user_name" name="session[user_name]" size="30" type="text" /> 
<label for="session_password">Password</label><br/> 
<input id="session_password" name="session[password]" size="30" type="password" /> 

、それはうまく動作します:私が手にエラーが

関連する生成されたHTMLがある
Failure/Error: fill_in :user_name, :with => @user.user_name 
    Webrat::NotFoundError: 
    Could not find field: :user_name 

です。エラーの原因となっている構文を使用したいので、何か間違っていますか?

+0

フォームの出力HTMLを含めてください。 –

+1

更新を参照してください... htmlの関連部分のみを追加しました。 –

答えて

2

これを試してみてください:

fill_in "user name", :with => @user.user_name 

私はwebratはいいことだと思うし、それは大文字と小文字が区別され、その「のパスワード」に:passwordを回すので、あなたのラベル「パスワード」を見つけることがそのこと。

また、あなたはむしろWebratよりもカピバラを使用するように切り替えを考えている場合、すなわち

fill_in :session_user_name, :with => @user.user_name 

一つの落とし穴ここでは、HTML id属性を使用することができ

:カピバラは大文字と小文字が区別されるので、fill_in "user name"に失敗します。

ベストプラクティスはここではわかりませんが、私自身のコードのラベルテキストではなく、HTML idname属性を使用するように変更しました。これは、セマンティック要素や名前を変更するよりも、サイト上の顧客対応のテキストを変更するため、脆弱ではないということです。

関連する問題