Rspec似乎正在通过应该失败的测试部分[Rails 4 In Action]

时间:2016-03-02 04:40:41

标签: ruby-on-rails ruby rspec

我正在通过Bigg,Katz和Klabnik的Rails 4 In Action工作,就个人而言,这是我对rspec测试平台的第一次介绍。

我遇到的情况是文本中提到的预期失败原因 NOT 与我收到的失败消息一致。事实上,我的失败似乎发生在后来的一行,这让我觉得rspec正在传递应该失败的部分。

文本期望测试拒绝的问题是失败/错误:click_link“新票证”/ NoMethodError:/未定义的方法'门票'为yadda yadda。这完全合情合理,因为我尚未将此方法编码到故障单控制器中。

相反,我收到了与缺少的“标题”字段相关的错误以及缺少“创建故障单”按钮,如下面的代码所示,是在对上述故障单方法进行rspec测试之后。

提前感谢您的见解!

现在到代码...让我知道是否还有其他的东西让我服务

creating_tickets_spec.rb

在顶部,作为之前操作的一部分,click_link“New Ticket”

require 'spec_helper'

feature "Creating tickets" do
  before do
    FactoryGirl.create(:project, name: "Internet Explorer")

    visit '/'
    click_link "Internet Explorer"
    click_link "New Ticket"
  end

  scenario "Creating a ticket" do
    fill_in "Title", with: "Non-standards compliance"
    fill_in "Description", with: "My pages are ugly!"
    click_button "Create Ticket"

    expect(page).to have_content("Ticket has been created.")
  end

  scenario "Creating a ticket without valid attributes fails" do
    click_button "Create Ticket"

    expect(page).to have_content("Ticket has not been created.")
    expect(page).to have_content("Title can't be blank")
    expect(page).to have_content("Description can't be blank")
  end
end

show.html.erb

<% title(@project.name, "Projects") %>

<h2><%= @project.name %></h2>

<%= link_to "Edit Project", edit_project_path(@project) %>

<%= link_to "Delete Project",
    project_path(@project),
    method: :delete, # This is intentionally listed as delete rather than the
    # standard destroy method of CRUD.
    data: { confirm:
              "Are you sure you want to delete this project?"
          } %>

<%= link_to "New Ticket", new_project_ticket_path(@project) %>

<!-- This is something I added on my own -->
</br>
<%= link_to "Home", projects_path %>

tickets.controller.rb

class TicketsController < ApplicationController

  before_action :set_project

  def new
    @ticket = @project.tickets.build
  end

  private
    def set_project
      @project = Project.find(params[:project_id])
    end

end

project.rb

class Project < ActiveRecord::Base
  validates :name, presence: true
  has_many :tickets
end

的Gemfile

请注意,我在文字

推荐的范围之外添加了一些额外的宝石
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'better_errors'
  gem 'minitest-reporters'
  gem 'rspec-rails', "~> 2.14"

end
group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  gem 'minitest-spec-rails'
  gem 'minitest-rails-capybara'
  gem 'capybara'
  gem 'factory_girl_rails', '~> 4.2.1'
end

的routes.rb

Rails.application.routes.draw do
  root "projects#index"

  resources :projects do
    resources :tickets    
  end
end

bin / rake路由的终端输出

Running via Spring preloader in process 58481
             Prefix Verb   URI Pattern                                      Controller#Action
               root GET    /                                                projects#index
    project_tickets GET    /projects/:project_id/tickets(.:format)          tickets#index
                    POST   /projects/:project_id/tickets(.:format)          tickets#create
 new_project_ticket GET    /projects/:project_id/tickets/new(.:format)      tickets#new
edit_project_ticket GET    /projects/:project_id/tickets/:id/edit(.:format) tickets#edit
     project_ticket GET    /projects/:project_id/tickets/:id(.:format)      tickets#show
                    PATCH  /projects/:project_id/tickets/:id(.:format)      tickets#update
                    PUT    /projects/:project_id/tickets/:id(.:format)      tickets#update
                    DELETE /projects/:project_id/tickets/:id(.:format)      tickets#destroy
           projects GET    /projects(.:format)                              projects#index
                    POST   /projects(.:format)                              projects#create
        new_project GET    /projects/new(.:format)                          projects#new
       edit_project GET    /projects/:id/edit(.:format)                     projects#edit
            project GET    /projects/:id(.:format)                          projects#show
                    PATCH  /projects/:id(.:format)                          projects#update
                    PUT    /projects/:id(.:format)                          projects#update
                    DELETE /projects/:id(.:format)                          projects#destroy

rspec返回

FF

Failures:

  1) Creating tickets Creating a ticket
     Failure/Error: fill_in "Title", with: "Non-standards compliance"
     Capybara::ElementNotFound:
       Unable to find field "Title"
     # ./spec/features/creating_tickets_spec.rb:13:in `block (2 levels) in <top (required)>'

  2) Creating tickets Creating a ticket without valid attributes fails
     Failure/Error: click_button "Create Ticket"
     Capybara::ElementNotFound:
       Unable to find button "Create Ticket"
     # ./spec/features/creating_tickets_spec.rb:21:in `block (2 levels) in <top (required)>'

Finished in 0.24689 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/features/creating_tickets_spec.rb:12 # Creating tickets Creating a ticket
rspec ./spec/features/creating_tickets_spec.rb:20 # Creating tickets Creating a ticket without valid attributes fails

Randomized with seed 33653

2 个答案:

答案 0 :(得分:0)

好的,首先,你得到的代码有正确的错误。

在浏览器中浏览测试以查看实际发生的情况...您访问该链接,单击&#34; Internet Explorer&#34;,然后单击按钮&#34;新票&#34;。

一个名为that的按钮,因此当您这样做时,规范不会失败。

浏览器将正确地将您带到标准的Rails故障页面。

rspec + capybara对这种情况没有任何问题 - 它不是规范中的例外......你必须实际测试水豚中的真实错误页面 - 所以它们不算作规范的问题

以这种方式思考 - 如果我是一个自我意识的水豚实例,我会思考&#34;对,接下来我们将测试我们是否正确显示错误页面& #34; ;)

您在标题上失败的原因当然是错误页面没有带有名为title的字段的表单...

所以...继续执行新的票证方法并继续:)

答案 1 :(得分:0)

我怀疑我的结果不一致,所以我继续研究我的问题。事实证明我没有看到文本中提到的rspec返回的相同错误消息的原因是因为我将优秀的better_errors gem安装到:development和:test组。

显然,当在test:test中运行这个gem时,它会在有机会循环到rspec + capybara之前劫持错误输出。一旦我将宝石移动到仅仅是开发组的一部分,测试结果就与参考文本以及我认为Taryn和Josh认为我所看到的一致。