首页 > 解决方案 > 在 rspec 上使用 render_views 方法时出现路由问题

问题描述

我开始在 Ruby on Rails 上学习 TDD,并且遇到了这些我无法解决的问题,当我使用 render_views 方法检查视图上是否有数据时,我遇到了这个路由问题。(它的 render_views 方法没有问题)

Failures:

  1) CachorrosController GET index ter a variável de classe @cachorros
 Failure/Error: get :index

 SyntaxError:
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:7: syntax error, unexpected keyword_ensure, expecting keyword_end
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:9: syntax error, unexpected end-of-input, expecting keyword_end
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
 # ./spec/controllers/cachorros_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

  2) CachorrosController GET index esta rota precisa renderizar o template index
 Failure/Error: get :index

 SyntaxError:
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:7: syntax error, unexpected keyword_ensure, expecting keyword_end
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:9: syntax error, unexpected end-of-input, expecting keyword_end
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
 # ./spec/controllers/cachorros_controller_spec.rb:16:in `block (3 levels) in <top (required)>'

  3) CachorrosController GET index verifica se o código foi inserido na página
 Failure/Error: get :index

 SyntaxError:
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:7: syntax error, unexpected keyword_ensure, expecting keyword_end
   /vagrant/Aulas - Torne-se um programador/app_gem/app/views/cachorros/index.html.erb:9: syntax error, unexpected end-of-input, expecting keyword_end
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/template_assertions.rb:61:in `process'
 # /home/vagrant/.rvm/gems/ruby-2.3.7/gems/rails-controller-testing-1.0.4/lib/rails/controller/testing/integration.rb:13:in `block (2 levels) in <module:Integration>'
 # ./spec/controllers/cachorros_controller_spec.rb:23:in `block (3 levels) in <top (required)>'

Finished in 0.28037 seconds (files took 5.17 seconds to load)
10 examples, 3 failures

Failed examples:

rspec ./spec/controllers/cachorros_controller_spec.rb:7 # CachorrosController GET index ter a variável de classe @cachorros
rspec ./spec/controllers/cachorros_controller_spec.rb:15 # CachorrosController GET index esta rota precisa renderizar o template index
rspec ./spec/controllers/cachorros_controller_spec.rb:20 # CachorrosController GET index verifica se o código foi inserido na página

这是规范/控制器/cachorros_controller_spec.rb:

require 'rails_helper'

RSpec.describe CachorrosController, type: :controller do
  describe "GET index" do
    render_views

    it "ter a variável de classe @cachorros" do
      Cachorro.destroy_all
      cachorro = Cachorro.create(nome: "Boomer", raca: "Beagle")
      get :index
      expect(assigns(:cachorros)).to eq([cachorro])
      expect(response.status).to eq(200)
    end

    it "esta rota precisa renderizar o template index" do
      get :index
      expect(response).to render_template("index")
    end

    it "verifica se o código foi inserido na página" do
      Cachorro.destroy_all
      cachorro = Cachorro.create(nome: "Boomer", raca: "Beagle")
      get :index
      expect(response.body).to match /<ul>.?*<li>/im
    end
  end
end

这是视图/cachorros/index.html.erb:

<ul>
  <% @cachorros.each do |cachorro| %>
  <li> <%= @cachorro.nome %></li>
</ul>

标签: ruby-on-railsrspecroutingtddrendering

解决方案


它缺少end迭代

<ul>
  <% @cachorros.each do |cachorro| %>
    <li> <%= @cachorro.nome %></li>
  <% end %>
</ul>

推荐阅读