Rails testing non-AR model(class) with FactoryGirl - ruby-on-rails

I have a class that looks like this:
class Killmail::Parser
def initialize(body)
#body = body
end
end
I also have a factory for it:
FactoryGirl.define do
factory :parser, class: Killmail::Parser do
skip_create
body '2013.12.02 19:24 bla bla'
initialize_with { new(attributes) }
end
end
It all works fine as long as I don't try to change default attributes. However when I try to use it like this
FactoryGirl.create(:parser, body: 'some different body')
It returns this:
=> #<Killmail::Parser:0x007fb2ff116548 #body={:body=>"some different body"}>
What am I doing wrong? Can't really google anything useful on this case.

Have you tried calling new(body) instead of new(attributes)? The problem could lie with how you're initializing your Parser class.
attributes creates a hash of all the attributes and passes that...
initialize_with { new(attributes) }
# roughly translates to
Parser.new({ body: '2013.12.02 19:24 bla bla' })
While calling #new with just body passes just that value as the first param...
initialize_with { new(body) }
# roughly translates to
Parser.new('2013.12.02 19:24 bla bla')
And you can add as many values to #new as you want in the same fashion...
# in your FactoryGirl.define
new_var_1 'new_string_1'
new_var_2 2
initialize_with { new(body, new_var_1, new_var_2) }
# roughly translates to
Parser.new('2013.12.02 19:24 bla bla', 'new_string_1', 2)

This will work, although it's a bit fiddly:
Change
initialize_with { new(attributes) }
To
initialize_with { new(attributes[:body]) }
I don't think factory girl was designed for this sort of thing TBH

Related

Manually set FactoryBot's sequence value

I use FactoryBot's sequence method to generate unique values, which work great in tests.
Example:
FactoryBot.define do
factory :my_factory do
sequence(:label) { |n| "Label #{n}" }
end
end
However, if I try to generate records in my dev environment, I hit lots of errors because the sequence counter has reset. I would love to set it manually, something like set_sequence(123456), is anything like that possible?
Sequence accepts a seed value parameter so something like this should work
FactoryBot.define do
factory :my_factory do
sequence(:label, 123456) { |n| "Label #{n}" }
end
end
You could now set the start value in your configuration
# config/environments/production.rb
Rails.application.configure do
config.x.factory_bot.sequence_seed_value = 1234
end
# config/environments/test.rb
Rails.application.configure do
config.x.factory_bot.sequence_seed_value = 0
end
FactoryBot.define do
factory :my_factory do
sequence(:label, Rails.configuration.x.factory_bot.sequence_seed_value) { |n| "Label #{n}" }
end
end
https://github.com/thoughtbot/factory_bot/blob/893eb67bbbde9d7f482852cc5133b4ab57e34b97/lib/factory_bot/sequence.rb#L13
https://github.com/thoughtbot/factory_bot/blob/893eb67bbbde9d7f482852cc5133b4ab57e34b97/spec/acceptance/sequence_spec.rb#L48
https://guides.rubyonrails.org/configuring.html#custom-configuration

RSpec and Factory Girl - Error with datetime field

I'm with the following problem:
Environment: Ruby: 2.3.1 and Rails 5.0.0.1
I'm trying to validate a datetime field with RSpec and Factory Girl.
I got this error:
expected: "2016-11-11 13:30:31 UTC" (From Factory Girl)
got: "2016-11-11T13:30:31.218Z" (From database)
My code:
klass_object = FactoryGirl.create(:character)
klass = Character
RSpec.shared_examples 'API GET #index' do |klass|
before { get :index, params: params, accept: Mime[:json] }
it "returns a list of #{klass.to_s.underscore.pluralize}" do
object_array = json(response.body)
klass_attributes = klass.attribute_names.without("id", "created_at", "updated_at").map(&:to_sym)
klass_attributes.each do |attribute|
object_array.each do |object|
expect(object[attribute].to_s).to eq(klass_object[attribute].to_s)
end
end
end
...
end
Factory:
FactoryGirl.define do
factory :character do
marvel_id { Faker::Number.number(6).to_i }
name { Faker::Superhero.name }
description { Faker::Hipster.paragraphs(1) }
modified { Faker::Date.between(DateTime.now - 1, DateTime.now) }
factory :invalid_character do
id ''
name ''
marvel_id ''
modified ''
end
end
end
How can I correct this problem?
I did that, it works but I think it is not so good. There is a better way to do it?
object_array.each do |object|
if ActiveSupport::TimeWithZone == klass_object[attribute].class
expect(object[attribute].to_datetime.strftime("%Y-%m-%d %H:%M:%S")).to eq(klass_object[attribute].to_datetime.strftime("%Y-%m-%d %H:%M:%S"))
else
expect(object[attribute].to_s).to eq(klass_object[attribute].to_s)
end
end
Thanks for your help.
I can suggest you to change your approach to compare the results. You can use approach, which based on the idea of the golden master.
In according to this approach you take a snapshot of an object, and then compare all future versions of the object to the snapshot.
In your case you can write json fixture first time, check that json is correct and compare it with result json next time.
For example
approved.json
[
{
"travel_time_seconds": 43200,
"available_seats_amount": 10,
"departure_at": "2016-04-08T02:00:00.000+03:00",
"arrival_at": "2016-04-08T17:00:00.000+03:00",
"source_point_name": "New York",
"destination_point_name": "Moscow",
"tickets_count": 2
}
]
controller_spec.rb
RSpec.shared_examples 'API GET #index' do |klass|
before { get :index, params: params, accept: Mime[:json] }
it "returns a list of #{klass.to_s.underscore.pluralize}" do
verify(format: :json) { json(response.body).map {|o| o.except('id', 'created_at', 'updated_at' }
end
...
end
approvals gem, for example, can help you with that
I know this is a very old question but I just came across the solution to this today and couldn't find another answer. I've been using Faker too, but the Date/Time formats don't seem to work very well with Ruby time math without a lot of finagling.
However, if you use Time in your factory, and then convert it .to_i, it will get sent to the db in the correct format for comparison in rspec.
Migration:
class CreateExperiences < ActiveRecord::Migration[5.2]
def change
create_table :experiences do |t|
t.datetime :start_date
Factory:
FactoryBot.define do
when_it_started = Time.zone.now - rand(3000).days
factory :experience do
start_date { when_it_started.to_i }
Spec:
RSpec.describe "Experiences API", type: :request do
let!(:valid_attributes) { attributes_for(:experience) }
describe "POST /v1/experiences" do
before { post "/v1/experiences", params: valid_attributes }
it "creates an experience" do
expect(JSON.parse(response.body).except("id", "created_at", "updated_at")).to eq(valid_attributes.stringify_keys)
end
Then my tests passed. Hopefully this will help someone else!
Try to use to_datetime instead to_s
expect(object[attribute].to_datetime).to eq(klass_object[attribute].to_datetime)

Sequence within trait of FactoryGirl factory does not use main sequence counter

I have the following factory:
FactoryGirl.define do
factory :foo do
sequence(:name) { |n| "Foo #{n}" }
trait :y do
sequence(:name) { |n| "Fooy #{n}" }
end
end
end
If I run
create :foo
create :foo
create :foo, :y
I get Foo 1, Foo 2, Fooy 1. But I want Foo1, Foo2, Fooy 3. How can I achieve this?
After a couple of hints from smile2day's answer and this answer, I came to the following solution:
FactoryGirl.define do
sequence :base_name do |n|
" #{n}"
end
factory :foo do
name { "Foo " + generate(:base_name) }
trait :y do
name { "Fooy " + generate(:base_name) }
end
end
end
You defined two different sequence generators since they are not within the same scope.
I would not use :name for the generator. A name that implies a number seems more suitable.
sequence :seq_number
Include a transient attribute in the factory and assign the generated sequence nunber.
transient do
seq_no { generate(:seq_number) }
end
Use the transient attribute for the 'name' attribute. The same applied to the trait version of 'name'.
name { "Foo #{seq_no}" }
trait :y do
name { "Fooy #{seq_no}" }
end
Cheers,
Eugen

How to test correctness of arguments sent to external API

I've external API endpoint, let's say: http://www.fake_me_hard.com/api. I would like to make some calls to this from my app.
Endpoint accepts following structure as argument:
{
:amount => amount,
:backurl => root_path,
:language => locale,
:orderid => order_id,
:pm => payment_method,
:accept_url => "/payment/success",
:exception_url => "/payment/failure",
}
For collecting this hash is responsible method EndpointRequestCollector.give_me_hash.
How I should test if give_me_hash returns proper structure ?
I can use the same strategy for creating this structure in specs and class as well so:
class EndpointRequestsCollector
def self.give_me_hash
{
#....collecting hash #1
}
end
end
describe EndpointRequestCollector do
context '.give_me_hash' do
it 'returns proper structure' do
expect(described_class.give_me_hash).to eq(
{
#... collecting hash #2
}
)
end
end
end
...but it would be repeating the same code in 2 places, and won't test anything.
Do you know any good approach to this problem ?
This is the way that i usually test my json api's:
If you just want to test the format, you can use include matcher:
%w(my awesome keys).each do |expected_key|
expect(described_class.give_me_hash.keys).to include(expected_key)
end
By doing this, you have the guarantee that the formar is correct, until someone break you method.
If you want to test the returned values, you can use something like that:
let(:correct_value) { 42 }
it 'must have correct value' do
expect(described_class.give_me_hash[key]). to eq correct_value
end
But i recomment you to separate this the logic to get the value to another method, and make another test just for it.
Perhaps:
let(:args) {["amount", "backurl", "language", "orderid", "pm", "accept_url", "exception_url"]}
#...
it 'returns proper structure' do
described_class.give_me_hash.each_key do |key|
expect(key).to satisfy{|key| args.include?(key)}
end
end

FactoryGirl: attributes_for not giving me associated attributes

I have a Code model factory like this:
Factory.define :code do |f|
f.value "code"
f.association :code_type
f.association(:codeable, :factory => :portfolio)
end
But when I test my controller with a simple test_should_create_code like this:
test "should create code" do
assert_difference('Code.count') do
post :create, :code => Factory.attributes_for(:code)
end
assert_redirected_to code_path(assigns(:code))
end
... the test fails. The new record is not created.
In the console, it seems that attributes_for does not return all required attributes like the create does.
rob#compy:~/dev/my_rails_app$ rails console test
Loading test environment (Rails 3.0.3)
irb(main):001:0> Factory.create(:code)
=> #<Code id: 1, code_type_id: 1, value: "code", codeable_id: 1, codeable_type: "Portfolio", created_at: "2011-02-24 10:42:20", updated_at: "2011-02-24 10:42:20">
irb(main):002:0> Factory.attributes_for(:code)
=> {:value=>"code"}
Any ideas?
Thanks,
You can try something like this:
(Factory.build :code).attributes.symbolize_keys
Check this: http://groups.google.com/group/factory_girl/browse_thread/thread/a95071d66d97987e)
This one doesn't return timestamps etc., only attributes that are accessible for mass assignment:
(FactoryGirl.build :position).attributes.symbolize_keys.reject { |key, value| !Position.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) }
Still, it's quite ugly. I think FactoryGirl should provide something like this out of the box.
I opened a request for this here.
I'd suggest yet an other approach, which I think is clearer:
attr = attributes_for(:code).merge(code_type: create(:code_type))
heres what I end up doing...
conf = FactoryGirl.build(:conference)
post :create, {:conference => conf.attributes.slice(*conf.class.accessible_attributes) }
I've synthesized what others have said, in case it helps anyone else. To be consistent with the version of FactoryGirl in question, I've used Factory.build() instead of FactoryGirl.build(). Update as necessary.
def build_attributes_for(*args)
build_object = Factory.build(*args)
build_object.attributes.slice(*build_object.class.accessible_attributes).symbolize_keys
end
Simply call this method in place of Factory.attributes_for:
post :create, :code => build_attributes_for(:code)
The full gist (within a helper module) is here: https://gist.github.com/jlberglund/5207078
In my APP/spec/controllers/pages_controllers_spec.rb I set:
let(:valid_attributes) { FactoryGirl.attributes_for(:page).merge(subject: FactoryGirl.create(:theme), user: FactoryGirl.create(:user)) }
Because I have two models associated. This works too:
FactoryGirl.define do
factory :page do
title { Faker::Lorem.characters 12 }
body { Faker::Lorem.characters 38 }
discution false
published true
tags "linux, education, elearning"
section { FactoryGirl.create(:section) }
user { FactoryGirl.create(:user) }
end
end
Here's another way. You probably want to omit the id, created_at and updated_at attributes.
FactoryGirl.build(:car).attributes.except('id', 'created_at', 'updated_at').symbolize_keys
Limitations:
It does not generate attributes for HMT and HABTM associations (as these associations are stored in a join table, not an actual attribute).
Association strategy in the factory must be create, as in association :user, strategy: :create. This strategy can make your factory very slow if you don't use it wisely.

Resources