2012-06-30 53 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