2012-06-30 56 views
0

我有一個非常簡單的RSpec水豚have_selector(),似乎並沒有工作,我無法弄清楚爲什麼......但可能是簡單的東西。RSpec水豚have_selector測試不工作

這裏的RSpec的要求測試:

describe 'with valid information' 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
    fill_in 'Email', with: user.email 
    fill_in 'Password, with: user.password 
    click_button 'Login' 
    end 
    it { should have_selector('p', text: user.first_name) } 
    ... 
end 

new.html.erb的會議,我只是有這樣的事情:

<% if logged_in? %> 
    <p><%= current_user.first_name %></p> 
<% else %> 
    <%= form_for(:session, url: sessions_path) do |f| %> 
    ... 
<% end %> 

登錄表單完全在瀏覽器session#new作品。我可以很好地登錄,並能夠在p標籤中查看名字。問題似乎是水豚部分,因爲其他it測試檢查頁面上的h1標記(無論登錄是否存在)都可以正常工作。

有誰知道這個問題可能是什麼?

在此先感謝您的幫助!

+0

您是否檢查過呈現的HTML? –

回答

2

由於某種原因,您的登錄失敗的可能性很大。

更改您的測試看起來像這樣:

it "should show me my first name" do 
    save_and_open_page 
    should have_selector('p', text: user.first_name) 
end 

,將打開你的瀏覽器,並顯示你的頁面作爲測試瀏覽器目前看到它。 (您可能需要將launchy寶石添加到您的Gemfile。)

還有一些其他的診斷,這將有助於,例如,添加測試或puts,檢查你降落在哪個頁面,如:

it "redirects to the home page after log in" do 
    current_path.should == root_path 
end 
+0

感謝您的提示!我嘗試過使用'save_and_open_page',它顯示了登錄頁面,儘管前面的方法使用Capybara登錄用戶。我不認爲登錄正在工作,但我不知道爲什麼,因爲它在現實生活中工作... – Justin

+0

事實證明,我忘記創建一個新用戶後,添加'before_save'創建一個令牌......但是,我正在給你答案,因爲這是我找到解決方案的原因! – Justin

0
fill_in 'Password, with: user.password 

不是那樣的。您應該使用此行,

fill_in 'Password', with: user.password 
+0

對不起,是的,這是我的錯誤,但不是在應用程序中。 – Justin