【发布时间】:2021-04-27 14:20:00
【问题描述】:
我的路由中有一个可选的语言环境参数。
# config/routes.rb
scope '(:locale)' do
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store_index', via: :all
end
# config/initializers/i18n.rb
#encoding: utf-8
I18n.default_locale = :en
LANGUAGES = [
['English', 'en'],
["Español".html_safe, 'es']
]
如果我想这样测试我的观点,
# /spec/views/carts/edit.html.erb_spec.rb
require 'rails_helper'
RSpec.describe "carts/edit", type: :view do
let(:cart) { create(:cart) }
before(:each) do
assign(:cart, cart)
end
it "renders the edit cart form" do
render
assert_select "form[action=?][method=?]", cart_path(cart), "post" do
end
end
end
我收到一个错误
/Users/burak/.rvm/rubies/ruby-2.7.3/bin/ruby -I/Users/burak/.rvm/gems/ruby-2.7.3/gems/rspec-core-3.10.1/lib:/Users/burak/.rvm/gems/ruby-2.7.3/gems/rspec-support-3.10.2/lib /Users/burak/.rvm/gems/ruby-2.7.3/gems/rspec-core-3.10.1/exe/rspec spec/views/carts/edit.html.erb_spec.rb
F
Failures:
1) carts/edit renders the edit cart form
Failure/Error: <%= form_with(model: cart) do |form| %>
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"carts", :format=>nil, :locale=>#<Cart id: 1252, created_at: "2021-04-27 13:45:09.842106000 +0000", updated_at: "2021-04-27 13:45:09.842106000 +0000">}, missing required keys: [:id]
Did you mean? cart_url
carts_url
carts_path
new_cart_url
# ./app/views/carts/_form.html.erb:1:in `_app_views_carts__form_html_erb__2203507052991398875_16560'
# ./app/views/carts/edit.html.erb:3:in `_app_views_carts_edit_html_erb___4312857554407673387_16540'
# ./spec/views/carts/edit.html.erb_spec.rb:11:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# ActionController::UrlGenerationError:
# No route matches {:action=>"show", :controller=>"carts", :format=>nil, :locale=>#<Cart id: 1252, created_at: "2021-04-27 13:45:09.842106000 +0000", updated_at: "2021-04-27 13:45:09.842106000 +0000">}, missing required keys: [:id]
# Did you mean? cart_url
# carts_url
# carts_path
# new_cart_url
# ./app/views/carts/_form.html.erb:1:in `_app_views_carts__form_html_erb__2203507052991398875_16560'
Finished in 0.02277 seconds (files took 0.60597 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/views/carts/edit.html.erb_spec.rb:10 # carts/edit renders the edit cart form
据我了解,那是因为我的购物车对象正在通过 :locale 关键字传递到某个地方。如果我在我的_form 部分中明确地将 URL 传递给form_with,那么错误就会消失,但这会破坏其他一些测试。我该如何解决这个问题?
这里是必要的部分和视图。
# app/views/carts/edit.html.erb
<h1>Editing Cart</h1>
<%= render 'form', cart: @cart %>
<%= link_to 'Show', @cart %> |
<%= link_to 'Back', carts_path %>
# app/views/carts/_form.html.erb
<%= form_with(model: cart) do |form| %>
<% if cart.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(cart.errors.count, "error") %> prohibited this cart from being saved:</h2>
<ul>
<% cart.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
【问题讨论】:
标签: ruby-on-rails rspec ruby-on-rails-6