Я пытаюсь написать спецификации для моего приложения Pinterest клон. Мое приложение работает как положено, но не проходит мои тесты. Что я не понимаю?
pin_controller_spe c
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)
Также сказано: «Отрисовка шаблона была предотвращена rspe c -rails. Используйте render_views
для проверки содержимого визуализированного представления при необходимости. Не знаете, что это значит? Я попытался поместить render_template
с render_views
в `pin_controller_spe c .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