Getting rails tests to reflect actual behaviour with FriendlyID

83 Views Asked by At

The following class has only one mandatory relation and one validation

class Shop < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged

  validates :name, presence: true, uniqueness: true
end

When testing an update

test "should update shop" do
  patch shop_url(@shop), params: { shop: { name: (@shop.name + "shh"), nation_id: @shop.nation_id } }
  assert_redirected_to shop_url(@shop)
end

will fail with

ShopsControllerTest#test_should_update_shop [/[...]/test/controllers/shops_controller_test.rb:48]:
Expected response to be a redirect to <http://www.example.com/shops/39503638> but was a redirect to <http://www.example.com/shops/bare-minimumshh>.
Expected "http://www.example.com/shops/39503638" to be === "http://www.example.com/shops/bare-minimumshh".

The first Expected is the designed behaviour so the testing expectation is wrong.

It's a bit of a conundrum because the controller defines, per FriendlyID requirement

@shop = Shop.friendly.find(params[:id])

whereas the test invokes in the setup

@shop = shops(:bare_minimum)

How can this test be written to take into account the (new) slug?

0

There are 0 best solutions below