【问题标题】:RoR, Failure/Error: expect { click_button submit }.not_to change(User, :count)RoR,失败/错误:期望 { click_button submit }.not_to change(User, :count)
【发布时间】:2015-02-07 00:47:50
【问题描述】:

我卡在 Michael Hartl 的 Ruby on Rails 教程的第 7 章。

我的路线.rb

SampleApp::Application.routes.draw do
  get "users/new"
  resources :users
  root  'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
end

我的规范/请求/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "signup page" do
    before { visit signup_path }

    let(:submit) { "Create my account" }
    let(:submit) { "Sign up" }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end

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

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "with invalid information" do
    it "should not create a user" do
      expect { click_button submit }.not_to change(User, :count)
    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
  end
end

我的应用程序/views/users/new.html.erb

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>

      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

当我尝试运行测试时,我得到了这个:

gvyntyk@gvyntyk-r60:~/rails_projects/sample_app$ bundle exec rspec spec/
...............FF......................

Failures:

  1) User pages with invalid information should not create a user
     Failure/Error: expect { click_button submit }.not_to change(User, :count)
     NameError:
       undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_3:0xad40120>
     # ./spec/requests/user_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:27:in `block (3 levels) in <top (required)>'

  2) User pages with valid information should create a user
     Failure/Error: fill_in "Name",         with: "Example User"
     Capybara::ElementNotFound:
       Unable to find field "Name"
     # ./spec/requests/user_pages_spec.rb:33:in `block (3 levels) in <top (required)>'

Finished in 1.54 seconds
39 examples, 2 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:26 # User pages with invalid information should not create a user
rspec ./spec/requests/user_pages_spec.rb:39 # User pages with valid information should create a user

谁能解释一下为什么这个测试失败了?

【问题讨论】:

  • 不应该是click_button :submit吗?
  • 打扰一下……什么意思?在期望 { click_button :submit } 上编辑期望 { click_button submit }?
  • 我的意思是你正在尝试执行未定义的submit 方法,而你应该传递一个符号:submit
  • 抱歉,我不明白我在哪里弄错了。现在按钮提交(“创建我的帐户”)正常工作,但未通过测试。

标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails


【解决方案1】:

请确保您处于测试应该通过的章节中。更改代码后,有些测试不应该通过。 此外,您的第二个错误似乎是身份验证问题。无法找到“名称”字段可能意味着在运行此测试时,用户应该有 sign_up。我不太确定您发布的片段,但请确保您的所有缩进都是正确的,并且您的“末端”在正确的位置关闭。您现在拥有代码的方式看起来不像您在执行“使用有效信息”之前注册。尝试将描述“注册页面”的“结尾”放在“描述“用户页面”的“结尾”之前的最底部。

【讨论】:

  • 我假设它...在成功注册之前通过了测试。
【解决方案2】:

来自click_button docs:

通过 id、文本、值或标题查找按钮并单击它。

因此您可以为click_button 方法提供按钮值。而不是:

expect { click_button submit }.not_to change(User, :count)

应该是:

expect { click_button 'Create my account' }.not_to change(User, :count)

你会收到这个错误:

Failure/Error: fill_in "Name",         with: "Example User"
Capybara::ElementNotFound:
  Unable to find field "Name"

因为你忘记了visit signup_path

describe "with invalid information" do

describe "with valid information" do

块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2014-01-09
    相关资源
    最近更新 更多