ruby-on-rails - 编写 RSPEC/失败的测试。我的 rspec 文件有什么问题?
问题描述
我正在尝试为我的 Pinterest 克隆应用程序编写规范。我的应用程序按预期工作,但未通过我的测试。我不明白什么?
pin_controller_spec
require 'spec_helper'
require 'rspec-rails'
RSpec.describe PinsController do
describe "GET index" do
it 'renders the index template' do
get :library
expect(response).to render_template("index")
end
it 'populates @pins with all pins' do
get :index
expect(assigns[:pins]).to eq(Pin.all)
end
end
describe "GET new" do
it 'responds with successfully' do
get :new
expect(response.success?).to be(true)
end
it 'renders the new view' do
get :new
expect(response).to render_template(:new)
end
it 'assigns an instance variable to a new pin' do
get :new
expect(assigns(:pin)).to be_a_new(Pin)
end
end
describe "POST create" do
before(:each) do
@pin_hash = {
title: "Rails Wizard",
url: "http://railswizard.org",
slug: "rails-wizard",
text: "A fun and helpful Rails Resource",
resource_type: "rails"}
end
after(:each) do
pin = Pin.find_by_slug("rails-wizard")
if !pin.nil?
pin.destroy
end
end
it 'responds with a redirect' do
post :create, pin: @pin_hash
expect(response.redirect?).to be(true)
end
it 'creates a pin' do
post :create, pin: @pin_hash
expect(Pin.find_by_slug("rails-wizard").present?).to be(true)
end
it 'redirects to the show view' do
post :create, pin: @pin_hash
expect(response).to redirect_to(pin_url(assigns(:pin)))
end
it 'redisplays new form on error' do
# The title is required in the Pin model, so we'll
# delete the title from the @pin_hash in order
# to test what happens with invalid parameters
@pin_hash.delete(:title)
post :create, pin: @pin_hash
expect(response).to render_template(:new)
end
it 'assigns the @errors instance variable on error' do
# The title is required in the Pin model, so we'll
# delete the title from the @pin_hash in order
# to test what happens with invalid parameters
@pin_hash.delete(:title)
post :create, pin: @pin_hash
expect(assigns[:errors].present?).to be(true)
end
end
end
require "spec_helper"
RSpec.describe "Our Application Routes" do
describe "GET /pins/name-:slug" do
it 'renders the pins/show template' do
pin = Pin.first
get "/pins/name-#{pin.slug}"
expect(response).to render_template("pins/show")
end
it 'populates the @pin variable with the appropriate pin' do
pin = Pin.first
get "/pins/name-#{pin.slug}"
expect(assigns[:pin]).to eq(pin)
end
end
end
控制台日志
Failures:
1) PinsController GET index renders the index template
Failure/Error: get :library
ActionController::UrlGenerationError:
No route matches {:action=>"library", :controller=>"pins"}
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_dispatch/journey/formatter.rb:57:in `generate'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:744:in `generate'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:775:in `generate'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:770:in `generate_extras'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:494:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:395:in `get'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
2) PinsController POST create responds with a redirect
Failure/Error: post :create, pin: @pin_hash
ArgumentError:
unknown keyword: pin
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:460:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:403:in `post'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:52:in `block (3 levels) in <top (required)>'
3) PinsController POST create creates a pin
Failure/Error: post :create, pin: @pin_hash
ArgumentError:
unknown keyword: pin
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:460:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:403:in `post'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:57:in `block (3 levels) in <top (required)>'
4) PinsController POST create redirects to the show view
Failure/Error: post :create, pin: @pin_hash
ArgumentError:
unknown keyword: pin
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:460:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:403:in `post'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:62:in `block (3 levels) in <top (required)>'
5) PinsController POST create redisplays new form on error
Failure/Error: post :create, pin: @pin_hash
ArgumentError:
unknown keyword: pin
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:460:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:403:in `post'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:71:in `block (3 levels) in <top (required)>'
6) PinsController POST create assigns the @errors instance variable on error
Failure/Error: post :create, pin: @pin_hash
ArgumentError:
unknown keyword: pin
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:460:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.2/lib/action_controller/test_case.rb:403:in `post'
# /Users/dl/.rvm/gems/ruby-2.6.3/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/pins_controller_spec.rb:80:in `block (3 levels) in <top (required)>'
7) Our Application Routes GET /pins/name-:slug renders the pins/show template
Failure/Error: get "/pins/name-#{pin.slug}"
NoMethodError:
undefined method `slug' for nil:NilClass
# ./spec/requests/requests_spec.rb:8:in `block (3 levels) in <main>'
8) Our Application Routes GET /pins/name-:slug populates the @pin variable with the appropriate pin
Failure/Error: get "/pins/name-#{pin.slug}"
NoMethodError:
undefined method `slug' for nil:NilClass
# ./spec/requests/requests_spec.rb:13:in `block (3 levels) in <main>'
Finished in 1.63 seconds (files took 4.26 seconds to load)
12 examples, 8 failures
Failed examples:
rspec ./spec/controllers/pins_controller_spec.rb:5 # PinsController GET index renders the index template
rspec ./spec/controllers/pins_controller_spec.rb:51 # PinsController POST create responds with a redirect
rspec ./spec/controllers/pins_controller_spec.rb:56 # PinsController POST create creates a pin
rspec ./spec/controllers/pins_controller_spec.rb:61 # PinsController POST create redirects to the show view
rspec ./spec/controllers/pins_controller_spec.rb:66 # PinsController POST create redisplays new form on error
rspec ./spec/controllers/pins_controller_spec.rb:75 # PinsController POST create assigns the @errors instance variable on error
rspec ./spec/requests/requests_spec.rb:6 # Our Application Routes GET /pins/name-:slug renders the pins/show template
rspec ./spec/requests/requests_spec.rb:11 # Our Application Routes GET /pins/name-:slug populates the @pin variable with the appropriate pin
FProcessing by PinsController#index as HTML
Rendering pins/index.html.erb within layouts/application
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Rendered pins/index.html.erb within layouts/application (0.3ms)
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Completed 200 OK in 11ms (Views: 8.4ms | ActiveRecord: 0.0ms)
.Processing by PinsController#new as HTML
Rendering pins/new.html.erb within layouts/application
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Rendered pins/new.html.erb within layouts/application (215.7ms)
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Completed 200 OK in 219ms (Views: 216.7ms | ActiveRecord: 0.0ms)
DEPRECATION WARNING: The success? predicate is deprecated and will be removed in Rails 6.0. Please use successful? as provided by Rack::Response::Helpers. (called from block (3 levels) in <top (required)> at /Users/dl/Desktop/SkillCrush/RubyOnRails/Rails/rails-pinning-app/spec/controllers/pins_controller_spec.rb:20)
.Processing by PinsController#new as HTML
Rendering pins/new.html.erb within layouts/application
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Rendered pins/new.html.erb within layouts/application (0.3ms)
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Completed 200 OK in 2ms (Views: 1.1ms | ActiveRecord: 0.0ms)
.Processing by PinsController#new as HTML
Rendering pins/new.html.erb within layouts/application
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Rendered pins/new.html.erb within layouts/application (0.2ms)
Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.
Completed 200 OK in 2ms (Views: 0.9ms | ActiveRecord: 0.0ms)
还说“rspec-rails阻止了模板渲染。render_views
如有必要,请用于验证渲染的视图内容。不确定这意味着什么?我尝试放置render_template
在render_views
`pin_controller_spec.rb中但运气不佳。
pin_controller.rb
class PinsController < ApplicationController
#READ
def index
@pins = Pin.all
end
def show
@pin = Pin.find(params[:id])
end
def show_by_name
@pin = Pin.find_by_slug(params[:slug])
render :show
end
#CREATE
def new
@pin = Pin.new
end
def create
@pin = Pin.create(pin_params)
@pin.slug = create_slug(@pin)
@pin.save
if @pin.persisted?
redirect_to @pin
else
@errors = @pin.errors
render :new
end
end
#UPDATE
def edit
@pin = Pin.find(params[:id])
render :edit
end
def update
@pin = Pin.find(params[:id])
if @pin.update_attributes(pin_params)
redirect_to pin_path(@pin)
else
@errors = @pin.errors
render :edit
end
end
#DESTROY
def destroy
#@pin.delete(pin_params)
pin = Pin.find(params[:id])
pin.destroy
#pin.destroy(pin_params)
redirect_to root_path, :notice => "Your ppin has been deleted"
end
#PRIVATE METHODS
private
#pin_params
def pin_params
params.require(:pin).permit(:title, :url, :slug, :text, :category_id, :image)
end
#generate slug
def create_slug(pin)
pin_slug = pin.title.downcase.parameterize(separator: "-")
pin_slug
end
end
解决方案
推荐阅读
- opencv - Opencv(JS)从图像中选择手指区域
- api - 在 Windows 服务中自托管 ASPNET 核心 5.0 Web API 的问题
- sql-server - SQL 连接查询与“on”异常
- next.js - 从 next.js 项目上传源地图到哨兵的推荐方法是什么
- c++11 - 字符串化宏中的参数
- python - 我想将我的 Minecraft 控制台数据发送到我的应用程序中
- powershell - 在我的函数中使用 Switch 的问题
- amazon-web-services - 将秒数格式化为 Redshift 中的间隔 HH:MM:SS
- swift - Swift AutoDiff:我们如何使结构具有一个成员变量,该成员变量是多个参数的可微函数?
- mongodb - Mongoose $or 运算符无法正常工作