簡體   English   中英

RSpec Capybara have_selector測試不起作用

[英]RSpec Capybara have_selector Test Not Working

我有一個非常簡單的RSpec Capybara 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 #new在瀏覽器中完美運行。 我可以正常登錄,並能夠在p標簽中查看名字。 問題似乎是水豚部分,因為另一個it測試檢查頁面上的h1標簽(無論是否登錄都存在)工作正常。

有誰知道是什么問題?

在此先感謝您的幫助!!

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

將測試更改為如下所示:

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

這將打開您的瀏覽器,並在測試瀏覽器當前看到它時顯示該頁面。 (您可能需要將launchy gem添加到Gemfile中。)

還有一些其他診斷可以幫助,例如,添加測試或puts ,檢查您登陸的頁面,例如:

it "redirects to the home page after log in" do
  current_path.should == root_path
end
fill_in 'Password, with: user.password

不是這樣的。 你應該用這條線,

fill_in 'Password', with: user.password

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM