首页 文章

RSpec Capybara have_selector测试不起作用

提问于
浏览
0

我有一个非常简单的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 标签中查看名字 . 问题似乎是水豚部分,因为页面上的 h1 标签的其他 h1 测试检查(无论是否登录都存在)都可以正常工作 .

有谁知道是什么问题?

在此先感谢您的帮助!!

2 回答

  • 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
    
  • 0
    fill_in 'Password, with: user.password
    

    不是这样的 . 你应该用这条线,

    fill_in 'Password', with: user.password
    

相关问题