简体   繁体   English

如何使此rspec测试通过?

[英]How do I get this rspec test to pass?

I can't for the life of me figure out why these tests are failing. 我无法终生弄清楚这些测试为何失败。

When a user puts in their email/password and hits the Log in button, they are redirected to their profile page which puts their first name in the title and displays their first name on the page. 当用户输入电子邮件/密码并单击“登录”按钮时,他们将被重定向到其个人资料页面,该页面将其名字放在标题中并在页面上显示其名字。 It also shows a link to their profile and a sign out link. 它还显示了其个人资料的链接和注销链接。 When I went through the steps in the browser everything was where it should be, but when rspec runs it continues to fail. 当我完成浏览器中的步骤时,一切都应该在原处,但是当rspec运行时,它继续失败。

What I find really odd is that when I run a user_page_spec test that tests the same elements, those all pass. 我真正感到奇怪的是,当我运行一个测试相同元素的user_page_spec测试时,所有这些都通过了。

I figure it has to do with either the click_button part or the "redirect_to user" in the controller, but any insight would be much appreciated. 我认为这与控制器中的click_button部分或“ redirect_to user”有关,但是任何见解将不胜感激。

Here are the tests- 这是测试-

Passing tests in user_pages_spec.rb- 通过user_pages_spec.rb-中的测试

describe "profile page" do
    let(:user) {  FactoryGirl.create(:user)  }
    before {  visit user_path(user)  }

    it {  should have_selector('h1',    text: user.firstName)  }
    it {  should have_selector('title', text: user.firstName)  }
end

Failing tests in authentication_pages_spec.rb - require 'spec_helper' authentication_pages_spec.rb中的测试失败-需要'spec_helper'

describe "Authentication" do
    describe "sign in" do
    .
    .
    .
    describe "with valid information" do
        let(:user) {  FactoryGirl.create(:user)  }
        before do
            fill_in "Email",            with: user.email
            fill_in "Password",     with: user.password
            click_button "Log in"
        end

        it {  should have_selector('title', text:user.firstName)  }
        it {  should have_link('Profile', href: user_path(user))  }
        it {  should have_link('Sign out', href: signout_path)  }

        describe "followed by signout" do
            before {  click_link "Sign out"  }
            it {  should have_link('Home')  }
        end
    end
  end
end

Yup. 对。 It's always the simplest of oversights that cause the biggest of headaches. 总是最简单的监督会引起最大的麻烦。

Here is what happened. 这是发生了什么事。

Rather than using the following- 而不是使用以下内容-

describe "page" do
  it "should have something" do
    page.should have_selector('')
  end
end

Rspec lets you define a subject - Rspec可让您定义主题-

subject {  page  }

Which allows you to simplify the first code block to the following- 这使您可以将第一个代码块简化为以下代码:

subject {  page  }
describe "page" do
  it {  should have_selector('')  }
end

This allows you to run multiple tests which reference the page without all the extra typing. 这使您可以运行引用该页面的多个测试,而无需进行所有额外的输入。

I left out the subject { page } at the very top, so none of my it {} blocks knew what to reference. 我在最顶部省略了主题{page},所以我的it {}块都不知道要引用什么。 As soon as that was added in, all tests passed with no problems. 一经添加,所有测试均顺利通过。

Hope this helps someone else out in the future. 希望这对以后的人有所帮助。

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

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