如何通过这个rspec测试?

时间:2022-01-02 04:02:32

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用户”有关,但任何见解都会非常感激。

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

1 个解决方案

#1


0  

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. As soon as that was added in, all tests passed with no problems.

我在最顶层省略了主题{page},所以我的{}}块都没有知道要引用的内容。一旦添加,所有测试都没有问题。

Hope this helps someone else out in the future.

希望这有助于将来的其他人。

#1


0  

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. As soon as that was added in, all tests passed with no problems.

我在最顶层省略了主题{page},所以我的{}}块都没有知道要引用的内容。一旦添加,所有测试都没有问题。

Hope this helps someone else out in the future.

希望这有助于将来的其他人。