开发者

Friendly_id for fixtures (slugs)

开发者 https://www.devze.com 2022-12-23 18:05 出处:网络
I am using freindly_id in my application. I have setup a few fixtures in my application. I want to do integration testing on them. During the tests I need the friendly_id, but the DB records created f

I am using freindly_id in my application. I have setup a few fixtures in my application. I want to do integration testing on them. During the tests I need the friendly_id, but the DB records created from fixtures do not ha开发者_Go百科ve the corresponding slugs in the Slug table.

Aren't the slugs automatically created from Fixture data? If not then what can be a solution?


The easiest would be to add the slugs to the fixture data:

# test/fixtures/things.yml
apple:
  title: Apple
  restricted: hellyes
  slug: apple
# app/model/thing.rb
class Thing << ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
end


One solution is to run the rake task that generates the slugs, but in the test environment.

On Windows

>set RAILS_ENV=test
>rake friendly_id:redo_slugs MODEL=xxx

Or Unix/Linux

>export RAILS_ENV=test
>rake friendly_id:redo_slugs MODEL=xxx

Update The rake tasks have been removed, see comment to this answer or friendly_id README.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号