RSpec Capybara have_selector测试不起作用

时间:2012-06-30 23:03:41

标签: ruby-on-rails rspec capybara

我有一个非常简单的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在浏览器中完美运行。我可以正常登录,并且能够在p标签中查看名字。问题似乎是水豚部分,因为页面上it标记的其他h1测试检查(无论是否登录都存在)都可以正常工作。

有谁知道问题可能是什么?

提前感谢您的帮助!!

2 个答案:

答案 0 :(得分:2)

很可能,您的登录因某种原因而失败。

将测试更改为:

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

答案 1 :(得分:0)

fill_in 'Password, with: user.password

不是那样的。你应该使用这一行,

fill_in 'Password', with: user.password