2015-02-07 30 views
0

我在Michael Hartl的Ruby on Rails教程中被困在第7章中。RoR,失敗/錯誤:expect {click_button submit} .not_to change(User,:count)

我的routes.rb

SampleApp::Application.routes.draw do 
    get "users/new" 
    resources :users 
    root 'static_pages#home' 
    match '/signup', to: 'users#new',   via: 'get' 
    match '/help', to: 'static_pages#help', via: 'get' 
    match '/about', to: 'static_pages#about', via: 'get' 
    match '/contact', to: 'static_pages#contact', via: 'get' 
end 

我的規格/請求/ user_pages_spec.rb

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "signup page" do 
    before { visit signup_path } 

    let(:submit) { "Create my account" } 
    let(:submit) { "Sign up" } 

    it { should have_content('Sign up') } 
    it { should have_title(full_title('Sign up')) } 
    end 

    describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit user_path(user) } 

    it { should have_content(user.name) } 
    it { should have_title(user.name) } 
    end 

    describe "with invalid information" do 
    it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
    end 
    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 { click_button submit }.to change(User, :count).by(1) 
    end 
    end 
end 

我的應用程序/視圖/用戶/ new.html.erb

<% provide(:title, 'Sign up') %> 
<h1>Sign up</h1> 
<div class="row"> 
    <div class="span6 offset3"> 
    <%= form_for(@user) do |f| %> 

     <%= render 'shared/error_messages' %> 

     <%= f.label :name %> 
     <%= f.text_field :name %> 

     <%= f.label :email %> 
     <%= f.text_field :email %> 

     <%= f.label :password %> 
     <%= f.password_field :password %> 

     <%= f.label :password_confirmation, "Confirmation" %> 
     <%= f.password_field :password_confirmation %> 

     <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> 
    <% end %> 
    </div> 
</div> 

而且當我嘗試運行測試時,我得到:

[email protected]:~/rails_projects/sample_app$ bundle exec rspec spec/ 
...............FF...................... 

Failures: 

    1) User pages with invalid information should not create a user 
    Failure/Error: expect { click_button submit }.not_to change(User, :count) 
    NameError: 
     undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_3:0xad40120> 
    # ./spec/requests/user_pages_spec.rb:27:in `block (4 levels) in <top (required)>' 
    # ./spec/requests/user_pages_spec.rb:27:in `block (3 levels) in <top (required)>' 

    2) User pages with valid information should create a user 
    Failure/Error: fill_in "Name",   with: "Example User" 
    Capybara::ElementNotFound: 
     Unable to find field "Name" 
    # ./spec/requests/user_pages_spec.rb:33:in `block (3 levels) in <top (required)>' 

Finished in 1.54 seconds 
39 examples, 2 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:26 # User pages with invalid information should not create a user 
rspec ./spec/requests/user_pages_spec.rb:39 # User pages with valid information should create a user 

有人能解釋爲什麼這個測試失敗嗎?

+0

不應該是'click_button:submit'? – BroiSatse 2015-02-07 00:56:31

+0

不好意思......你是什麼意思?編輯期望{click_button submit}期待{click_button:submit}? – Gvyntyk 2015-02-07 01:08:02

+0

我的意思是你試圖執行未定義的'submit'方法,而你應該通過一個符號':submit' – BroiSatse 2015-02-07 01:11:06

回答

0

請確保您處於測試應該通過的章節中的某一點。有些測試在更改代碼後不應該通過。 此外,您的第二個錯誤似乎是一個身份驗證問題。無法找到字段「名稱」可能意味着在測試運行時,用戶應該有sign_up。我不太確定您發佈的片段,但請確保您的所有縮進都是正確的,並且您的「結束」會在正確的位置關閉。您現在擁有代碼的方式在執行「有效信息」之前似乎沒有註冊。嘗試將描述的「註冊頁面」的「結束」放置在「描述」用戶頁面「做」塊的「結束」之前的最底部。

+0

我認爲它......成功註冊前通過測試。 – Gvyntyk 2015-02-07 01:28:39

0

click_button文檔:

Finds a button by id, text, value or title and clicks it.

所以,你可以按鍵值提供給click_button方法。相反的:

expect { click_button submit }.not_to change(User, :count) 

它應該是:

expect { click_button 'Create my account' }.not_to change(User, :count) 

,您會收到此錯誤:

Failure/Error: fill_in "Name",   with: "Example User" 
Capybara::ElementNotFound: 
    Unable to find field "Name" 

因爲你忘了visit signup_path兩個:

describe "with invalid information" do 

describe "with valid information" do 

blocks。

相關問題