简体   繁体   English

rspec测试失败

[英]Failing rspec test

I have a failing Rspec test, _pages_spec.rb. 我的Rspec测试_pages_spec.rb失败。 I am following Micheal Hartl's Rails Tutorial. 我正在关注Micheal Hartl的Rails教程。

When I have the second "end" at line 83, all tests pass but one: 当我在第83行有第二个“结束”时,所有测试都通过了,但只有一个:

Failures:

  1) User pages signup edit with valid information
     ←[31mFailure/Error:←[0m ←[31mit { should have_selector('div.alert.alert-suc
cess') }←[0m
       ←[31mexpected css "div.alert.alert-success" to return something←[0m
←[36m     # ./spec/requests/user_pages_spec.rb:77:in `block (5 levels) in <top (
required)>'←[0m

Finished in 1.28 seconds
←[31m60 examples, 1 failure←[0m

Failed examples:

←[31mrspec ./spec/requests/user_pages_spec.rb:77←[0m ←[36m# User pages signup ed
it with valid information ←[0m

Here is the code: 这是代码:

require 'spec_helper'

describe "User pages" do

  subject { page }
  let (:base_title) {"Tom Gong's Sample App"}

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }
    it { should have_selector('h1',     text: user.name) }
    it { should have_selector('title',  text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }
    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', content: "#{base_title} | Sign up") }
  end
  describe "signup" do

    before { visit signup_path }
    let (:submit) { "Create my account" }
    describe "with invalid information" do
        it "should not create a user" do 
            expect { click_button submit }.not_to change(User, :count)
        end
      describe "after submission" do
        before { click_button submit}
        it { should have_selector('title', text: 'Sign up') }
        it { should have_content('error') }
      end
  end

  describe "with valid information" do

    before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
      fill_in "Password",     with: "foobar"
      fill_in "Confirmation", with: "foobar"
    end
    it "should create a user" do
      expect { click_button submit }. to change(User, :count).by(1)
    end
    describe "after saving the user" do
      before { click_button submit }
      let(:user) { User.find_by_email('user@example.com') }
      it { should have_selector('title', text:user.name) }
      it { should have_selector('div.alert.alert-success', text:'Welcome') }
    end
  end
  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit edit_user_path(user) }
    describe "page" do
      it { should have_selector('h1',     text: "Update your profile") }
      it { should have_selector('title',  text:"Edit user") }
      it { should have_selector('a', href: 'http://gravatar.com/emails') }
    end
    describe "with invalid information" do
      before { click_button "Save changes" }
      it { should have_content('error') }
    end
    describe "with valid information" do
      let(:new_name) { "New Name" }
      let(:new_email) { "new@example.com" }
      before do
        fill_in "Name",             with: new_name
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
    end

      it { should have_selector('title', text: new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { user.reload.name.should  == new_name }
      specify { user.reload.email.should == new_email }
    end
  end
end
end

Can anyone find the problem? 谁能找到问题?

User pages signup edit with valid information 用户页面注册使用有效信息进行编辑

Here's how it looks like: 看起来是这样的:

User pages
  |- signup
      |- edit
          |- with valid information

It should be 它应该是

User pages edit with valid information 用户页面使用有效信息进行编辑

ie

User pages
  |- edit
      |- with valid information

Currently edit is described under signup. 当前编辑在注册下描述。 That's not quite right. 那不是很正确。

I have the second "end" at line 83 我在第83行有第二个“结尾”

It should be right after the line 53 rather than 83. ie right before describe "edit" do . 它应该在53行之后而不是83行之后。即在describe "edit" do

With consistent indentation you don't need to guess where to put matching end , it should be obvious from code. 使用一致的缩进,您无需猜测匹配end ,这从代码中应该显而易见。

The error message is telling you that the page does not have the selector 'div.alert.alert-success'. It looks like the error is related to this line: 错误消息告诉您该页面没有选择器'div.alert.alert-success'. It looks like the error is related to this line: 'div.alert.alert-success'. It looks like the error is related to this line: it { should have_selector('div.alert.alert-success', text:'Welcome') }` 'div.alert.alert-success'. It looks like the error is related to this line:它{应该具有_selector('div.alert.alert-success',文本:'Welcome')}}

Capybara has a nifty method called save_and_open_page that lets you inspect the HTML file that Capybara parses. save_and_open_page有一个名为save_and_open_page的漂亮方法,可让您检查save_and_open_page解析的HTML文件。 Call this method and view the source to inspect the selector that is not working for you. 调用此方法并查看源以检查对您不起作用的选择器。 If you are still having trouble, paste the HTML that corresponds to the selector in your question. 如果仍然遇到问题,请在问题中粘贴与选择器相对应的HTML。

You should clean up the formatting of your code because it is very hard to read with the inconsistent spacing and indentation. 您应该清理代码的格式,因为空格和缩进不一致会很难阅读。

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

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