简体   繁体   English

RSpec Capybara have_selector测试不起作用

[英]RSpec Capybara have_selector Test Not Working

I have a very simple RSpec Capybara have_selector() that doesn't seem to be working and I can't figure out why... but probably something simple. 我有一个非常简单的RSpec Capybara have_selector()似乎没有工作,我无法弄清楚为什么......但可能是一些简单的东西。

Here's the RSpec request test: 这是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

In the new.html.erb for the session, I just have something like this: 在会话的new.html.erb中,我只是这样:

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

The login form session#new works perfectly in the browser. 登录表单session#new #new在浏览器中完美运行。 I can login just fine and am able to view the first name in a p tag. 我可以正常登录,并能够在p标签中查看名字。 The problem seems to be the capybara part, because the other it test checking for the h1 tag on the page (which is present regardless of being logged in) works fine. 问题似乎是水豚部分,因为另一个it测试检查页面上的h1标签(无论是否登录都存在)工作正常。

Does anyone know what the problem could be? 有谁知道是什么问题?

Thanks in advance for any help!! 在此先感谢您的帮助!!

More than likely, your login is failing for one reason or another. 您的登录很可能由于某种原因而失败。

Change your test to look like this: 将测试更改为如下所示:

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

That will open your browser and show you the page as the test browser currently sees it. 这将打开您的浏览器,并在测试浏览器当前看到它时显示该页面。 (You may need to add the launchy gem to your Gemfile.) (您可能需要将launchy gem添加到Gemfile中。)

There are a few other diagnostics that would help, for instance, adding a test or a puts that checks what page you landed on, such as: 还有一些其他诊断可以帮助,例如,添加测试或puts ,检查您登陆的页面,例如:

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

not like that. 不是这样的。 You should use this line, 你应该用这条线,

fill_in 'Password', with: user.password

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM