0

I have a model Post with an attribute published_at. However, in POST request API requires it to be assigned as published, not published_at. So in my model I override setter method like so

def published=(value)
  self.published_at = value
end

The problem is, in my API tests, I use FactoryGirl's attributes_for(:post) method to generate attributes for POST request, and it uses old published_at name. How can I workaround this problem and generate attributes with published key instead of published_at without changing the name of this attribute in my model?

evfwcqcg
  • 15,755
  • 15
  • 55
  • 72

2 Answers2

2

How about changing your :post factory:

factory :post do
  published { Time.now }
  # published_at
end

Or, if you want to separate API and Web based posts, create a new factory:

factory :api_post, class: Post do
  ...
end
melekes
  • 1,880
  • 2
  • 24
  • 30
2

I've never tried this myself, but it seems like an alias might work:

alias_attribute :published, :published_at

And in that case I would probably get rid of Post#published=.

Reference: https://stackoverflow.com/a/4017071/199712

Community
  • 1
  • 1
Jason Swett
  • 43,526
  • 67
  • 220
  • 351