2012-04-17 242 views
1

更新:Hartl Rails教程(3.2)8.2.5 Rspec失敗

我的測試沒有提交用戶信息。該初步認識代碼是在前面的章節的練習:

describe "after saving user" do 
    before { click_button submit } 
    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') } 
    it { should have_link('Profile') } 
    end 

/UPDATE

我已經完成了第8.2.5節(在註冊時簽到)和精確描述的應用程序的行爲:

  • 用戶在註冊後登錄
  • 然後重定向到其配置文件頁面
  • 其中標題已更改爲包括「退出」鏈接。

但我的'退出'鏈接測試失敗。這裏是我的代碼,從教程全部複製:

相關的控制器代碼(users_controller.rb):

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

相關的視圖代碼(_header.html.erb):

<ul class="dropdown-menu"> 
    <li><%= link_to "Profile", current_user %></li> 
    <li><%= link_to "Settings", '#' %></li> 
    <li class="divider"></li> 
    <li> 
    <%= link_to "Sign out", signout_path, method: "delete" %> 
    </li> 
</ul> 

相關測試代碼(user_pages_spec.rb):

describe "signup" do 

    before { visit signup_path } 

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

    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 

    describe "after saving user" do 
     it { should have_link('Profile') } 
    end 
    end 
end 

的錯誤是rspec ./spec/requests/user_pages_spec.rb:47 # User pages signup with valid information after saving user

謝謝!

+0

啊哈 - 不要忘記做測試點擊提交: – rda3000 2012-04-17 21:00:04

回答

1

我認爲最後的「描述」塊應該是這樣的:

describe "after saving user" do 
    before { click_button submit } 
    it { should have_content('Profile') } 
    end 

測試錯過了一下檢查,如果有一個頁面上相應的內容之前,「提交」按鈕。

相關問題