Active Record find from factory girl - ruby-on-rails

Im trying to implement a database-based sequence generator in rails so i wrote a code that goes something like
#semaphore.synchronize {
seq = Sequence.find_by_name(type)
seq.value += 1
seq.save
val = seq.value
unless prefix.nil?
"#{prefix}-#{val}"
else
"#{val}"
end
}
My question is, is it possible to setup the initial sequence data using factory girl and be able to access it using Sequence.find_by_name or fixture loading is my only option? i.e. rake db:fixtures:load RAILS_ENV=test FIXTURES_PATH=spec/fixtures?
Thanks

Ok, i was able to find the answer.
I created my Sequence definition in factory girl as below:
FactoryGirl.define do
factory :membership_sequence, class: Sequence do
name 'membership'
value 1
end
factory :payment_sequence, class: Sequence do
name 'payment'
value 1
end
end
Then, in the before(:all) I called the create() method
before(:all) do
# create the sequences
create(:membership_sequence)
create(:payment_sequence)
end
and voila! the Sequence.find_by_name shall work now.
To answer your question why i choose to implement sequence generation manually than using rails active record autogenerated ids, it's because i want to be able to generate sequences such as PREFIX-2015-0001, PREFIX-2016-0001

Related

Rails Rspec test with carrierwave image

I wrote an API which can return latest 5 Newsletter and its image, but I am stuck at writing its rspec test.
First of all, here is the relationship between model.
Newsletter has_many NewsletterImages
NewsletterImage belong_to Newsletter
Secondly, I thought that I need to create some data in test database, so I wrote following code in rspec file.
7.times do |i|
n = Newsletter.create(title: "Test#{i}", content: "TestContents#{i}")
2.times do |i|
ni = NewsletterImage.create(newsletter_id: n.id, order: i)
ni.image = File.open('xxx.png')
ni.save
end
end
So, I need to upload file in very test? Is there a better way to generate data and test?
Better to use Factory Girl to make your test data. That way, you can write clean tests like
# /spec/factories/newsletter_factory.rb
FactoryGirl.define do
factory :newsletter do
title "My newsletter"
content "Some content"
end
end
# /spec/factories/newsletter_image_factory.rb
FactoryGirl.define do
factory :newsletter_image do
newsletter
image fixture_file_upload( Rails.root + 'spec/fixtures/images/example.jpg', "image/jpg")
end
end
# spec/models/newsletter_spec.rb
image = create :newsletter_image
expect(image.newsletter.title).to eq 'My Newsletter'
With all of the details of how the models are created hidden in the factory definition files, it's then easy to share the code across many tests.
For more detail about adding carrierwave files to Factory Girl definitions, look for other answers such as this one: https://stackoverflow.com/a/9952914/693349

Test whether a class level method change attribute of another class

In the Ruby application I have a class(Resque Job) that has method that affect the values of a different class when called with the id of the latter class.
class ResqueKlass
def self.perform(id)
obj = EditKlass.find(id)
obj.update(value: 0)
end
end
I want to use rspec to test that this value was indeed changed within method
describe 'Something' do
let(:obj){FactoryGirl.create(:editklass)}
scenario 'Change obj value' do
ResqueKlass.perform(obj.id)
expect(obj.value).to eq(0)
end
end
This test fails where it expect 0 it get the value that was set in the factory girl.
I have also tried not using factory girl create 'obj' with let but that still does not work. I have placed bindings in the ResqueKlass perform method and i can see that the value is being updated.
PS please bear in mind that i am new to Ruby and rspec. These are not the exact classes that i am working with, the reason for that is the actual classes contain some sensitive data.
That happens, because you do not reload that record and therefore your obj still shows the old version.
Try reloading the obj with obj.reload:
describe 'Something' do
let(:obj){FactoryGirl.create(:editklass)}
scenario 'Change obj value' do
ResqueKlass.perform(obj.id)
expect(obj.reload.value).to eq(0)
end
end

Rspec/FactoryGirl uniqueness validations in a large test suite

Using Rspec and FactoryGirl, if I have a factory that autoincrements a trait using a sequence, and in some specs if I explicitly set this trait, with a large enough test suite, sometimes random specs fail with
Validation failed: uniq_id has already been taken
The factory is defined like this:
factory :user { sequence(:uniq_id) {|n| n + 1000} }
I'm guessing this validation fails because in one place in my test suite, I generate a user like this:
create(:user, uniq_id: 5555)
And because presumably factory girl is generating more than 4,555 users over the suite, the validation is failing?
I'm attempting to avoid this problem by just turning the uniq_id into 55555 (larger number), so there is no interference. But is there a better solution? My spec_helper includes these relevant bits:
config.use_transactional_fixtures = true
config.after(:all) do
DatabaseCleaner.clean_with(:truncation)
end
It happens to me sometimes. I didn't found any explanation, but happens only with big set of data. I let someone find the explanation!
When it happens, you can declare your attribute like this (here is an example using faker gem) :
FactoryGirl.define do
factory :user do
login do
# first attempt
l = Faker::Internet.user_name
while User.exists?(:login => l) do
# Here is a loop forcing validation
l = Faker::Internet.user_name
end
l # return login
end
end
end
I was able to solve my issue like this in my factory (based on #gotva's suggestion in the question comments).
factory :user do
sequence(:uniq_id) { |n| n + 1000 }
# increment again if somehow invalid
after(:build) do |obj|
if !obj.valid? && obj.errors.keys.include?(:uniq_id)
obj.uniq_id +=1
end
end
end

Factory_Girl not generating unique records

I'm converting my application over to use factories instead of fixtures with Factory_Girl_Rails. I have the following factory defined:
factory :requirement do
sequence(:reqTitle) {|t| "Test Requirement #{t}"}
ignore do
categoryName " "
categoryAbbr " "
end
reqText "This is a test general requirement for the purpose of, um, testing things"
status "Approved"
factory :reqWithCat do
category
end
factory :reqWithNamedCat do
category {create(:category, catName: categoryName, catAbbr: categoryAbbr)}
end
factory :reqFromUserRequirement do
user_requirement
end
end
Then, in the setup section, I run the following snippet:
(0..5).each do |x|
requirement = create(:reqWithCat)
requirement.ind_requirements {|ir| [create(:ind_requirements)]}
end
(0..5).each do |x|
create(:reqWithNamedCat, categoryName: "User Interface", categoryAbbr: "UI")
end
However, my tests are failing, apparently because records aren't being created (for instance, the index test on the requirements controller tells me that there 0 records returned when there should be 10). I run the tests in debug mode, and discover that every requirement created has the exact same id value. Also, each record has the same sequence value.
I believe the duplicate records are failing to save, which is why I'm getting a 0 return. However, I can't see what I've set up incorrectly. What am I missing here?
Once I fixed the user factories to create the related many properly, I then discovered that I had written this factory incorrectly based on the way I was defining scopes in my models. Once that was fixed, the "broken" test began working.

avoid factorygirl repeate the number when run second time in cucumber

Its my factorygirl code
FactoryGirl.define do
factory :account do
sequence :name do |n|
"Test Account#{n}"
end
end
end
This is my method for run factorygirl code
def create_accounts n=2
n.times do
FactoryGirl.create(:account, subscription_ids: #sub.id.to_s)
end
end
My problem is, first time my FactoryGirl output is Test Account1, Test Account2, When i execute second time, It create output as Test Account3, Test Account4. But I need Test Account1, Test Account2 when run multiple time. How may i do this.
Thanks for your advices
FactoryGirl is designed to create new unique records every time you call #create. If you want to keep the original record set around, you should save them to a variable and then return them rather than running FactoryGirl.create again.
You can also use database_cleaner gem to clean the database after every test. This helps to prevent any problems rising from the state of the database.
I solve this problem after replace this code
def create_accounts n=1
create_subscription
n.times do |r|
r += 1
FactoryGirl.create(:account, subscription_ids: #sub.id.to_s, name: "Test Account#{r}")
end
end
Updated
I am using cucumber-> capybara -> selenium
Reset the factory girl sequence
add this code in spec->support->reset.rb
module FactoryGirl
def self.reload
self.factories.clear
self.sequences.clear
self.traits.clear
self.find_definitions
end
end
Add this in env.rb
After do
FactoryGirl.reload
end

Resources