2013-04-08 69 views
0

我一直在努力使用ruby/rspec/capybara/devise来测试我的代码。一个简单的测试,我想写的,用于登录我有一个合法的用户登录,并期望看到如下面的代码中定义一个h1标签:你如何得到rspec输出它遇到的而不是它“没有找到预期的结果”?

describe "Authentication" do 

    subject { page } 
    describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { sign_in_with user.email } 

    it { should have_css('h1', text: "Welcome to the Test") } 
    end 
end 

问题是,我得到的回报是:

1) Authentication signin page with valid information 
Failure/Error: it { should have_css('h1', text: "Welcome to the Test") } 
    expected css "h1" with text "Welcome to the Test" to return something 
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>' 

有没有办法输出什么样的测试中发现的H1(或者说,它没有找到它呢?),而不是它没有找到什么期待?有一个很好的机会,我的sign_in方法不起作用,但我无法验证,因为我不确定在执行sign_in_with之后测试看到的是什么。

非常感谢并乐于提供更多的上下文,如果有帮助的话。

编辑 更新代码以反映测试主题。

回答

1

......我不确定在执行sign_in_with后测试看到的是什么。

您可以打开当前网页的快照与save_and_open_page

describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { sign_in_with user.email } 

    it { save_and_open_page; should have_css('h1', text: "Welcome to the Test") } 
end 
+0

我跑进唯一要注意的是,'save_and_open_page'需要的是在它的声明:HTTP :/ /stackoverflow.com/questions/12608976/save-and-open-page-capybara-launchy-stopped-working-in-a-project-error – Ryan 2013-04-08 17:12:15

+0

@Ryan好赶上,我已经更新了我的答案。 – Stefan 2013-04-09 08:09:07

0

没有主题,你不能用it来表示结果。对于Capybrara,您需要检查返回的对象page

让我们重新编写这样的测试:

describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
    sign_in_with user.email 
    end 
    it "signs in successfully" do 
    expect(page).to have_css('h1', text: "Welcome to the Test") 
    end   
end 

或更好,具有水豚的故事DSL

feature "User sign in" do 

    given(:user) { FactoryGirl.create(:user) } 

    scenario "with valid information " do 
    sign_in_with user.email 
    expect(page).to have_css('h1', text: "Welcome to the Test") 
    end   

    scenario "with invalid filing" do 
    sign_in_with "[email protected]" 
    expect(page).to have_text("Invalid email or password") 
    end 
end 
+0

对不起,它不明确,但页面设置为测试的主题。我已经更新了代码以包含该代码。但我的问题并不在于为什么我的测试失败的原因是我如何理解测试在页面中看到的内容,而不是它看不到预期的元素。 – Ryan 2013-04-08 16:57:44

+0

我觉得你的'subject'需要放在'describe'块和''before'之下。此外,我不喜欢在集成测试中使用'subject',这种风格将会使用很多简短的“块”,这在集成测试中会非常沉重。另外,短'it'块不适合集成测试,通常有几个步骤。 – 2013-04-08 17:02:10

+0

对于你的问题,这是关于水豚的设计,我不知道。但我认为这个逻辑是可以理解的。有许多动作,点击链接,点击按钮,悬停,滚动等,你不能自己检查动作,你只能检查他们带来的副作用 - 页面。 – 2013-04-08 17:08:18

相关问题