Rails: Unit testing a before_create?

I am trying to verify that a field is being generated properly by a callback, but I cannot figure out what it is.

album.rb

before_create :generate_permalink

private
  def generate_permalink
    @title = album.downcase.gsub(/\W/, '_')
    @artist = artist.downcase.gsub(/\W/, '_')
    self.permalink =  @artist + "-" + @title
  end

album_test.rb

test "should return a proper permalink" do
  album = Album.new(:artist=>'Dead Weathers', :album=>'Primary Colours')
  album.save
  assert_equal "dead_weathers-primary_colours", album.permalink 
end

But this does not work, because it album.permalinkwill not return the value if it is saved.

Is there any way to check a before_create? Should I do this at the controller level?

+3
source share
1 answer

I found this blog post that might interest you.

, , , . before_create

album.send(:before_create)
+4

Source: https://habr.com/ru/post/1710445/


All Articles