ruby-on-rails - 在 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>
解决方案
它缺少end
迭代
<ul>
<% @cachorros.each do |cachorro| %>
<li> <%= @cachorro.nome %></li>
<% end %>
</ul>
推荐阅读
- python - 未找到“xyz”的反向。“xyz”不是有效的视图函数或模式名称
- powershell - Windows 10 中的索引值 -1
- python - 在 Python 电子商务网站中更改产品排序
- python - 从父类(作为子类)调用违反封装的“私有”变量
- apache-kafka - 确认 kafka 记录,但没有数据返回给消费者
- html - 为什么我的网站在 Mozilla firefox 和 chrome 上看起来不一样?
- javascript - 如何使用 popper.js
- javascript - React - 如何解决:VS代码中的“JSX表达式必须有父级和表达式”错误
- android - 图像角半径
- python - Python3.7的参数扫描