0

私はカピバラを使用して統合テストを書いています。ここに私のテストのコードがあります。

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 %>

しかし、URL の最後のスラッシュが原因で、私のテストは失敗しました。

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

誰でも私を助けることができますか?

ありがとう。

4

1 に答える 1

0

POST /admin/countries問題は、フォームがエンドポイントを指すのではなく、表示ページを指すルートを使用していることです。

admin_countries_path2つのオプションがあります<%= form_tag [:admin, @country] do %>。the:adminはエンドポイントを参照するため、Rails はリソースのルート全体を正しく推測できます。

于 2013-10-26T12:04:57.143 に答える