我正在使用 capybara 编写集成测试,这里是我的测试代码。
test "when providing invalid data" do
admin = create(:administrator)
signin_administrator_as admin.email, admin.password
visit new_admin_country_path
click_button t("buttons.admin.save")
assert_equal admin_countries_path, current_path
assert page.has_text? t("admin.form_error_message")
end
在控制器中
def create
@country = Country.new(country_params)
if @country.save
redirect_to admin_countries_path, notice: t("flash.admin.country.create.notice")
else
render :new
end
end
我的表单有一部分用于插入和更新
<%= form_for @country, url: admin_country_path(@country) do |f| %>
<p>
<%= f.label :code, t("labels.country.code") %>
<%= f.text_field :code %>
</p>
<p>
<%= f.label :portuguese_name, t("labels.country.portuguese_name") %>
<%= f.text_field :portuguese_name %>
</p>
<p>
<%= f.label :spanish_name, t("labels.country.spanish_name") %>
<%= f.text_field :spanish_name %>
</p>
<p>
<%= f.label :english_name, t("labels.country.english_name") %>
<%= f.text_field :english_name %>
</p>
<%= f.submit t("buttons.admin.save") %>
<% end %>
但是我的测试失败了,因为网址的最后一个斜杠。
Expected: "/admin/countries"
Actual: "/admin/countries/"
这是我的路线定义
namespace :admin do
root to: "signin#new"
get "/signin", to: "signin#new"
post "/signin", to: "signin#create"
get "/dashboard", to: "dashboard#index"
resources :countries
end
谁能帮我?
谢谢。