Factory Girl created objects not clearing in between tests? - ruby-on-rails

I'm being held up implementing tests with a slight confusion. With User.create I can create and save in multiple tests:
should "test something" do
u1 = User.create(:first_name => "Fred", :last_name => "Flintstone")
assert true
end
should "test something else" do
u1 = User.create(:first_name => "Fred", :last_name => "Flintstone")
assert true
end
but using Factory.create, it throws a ActiveRecord duplicate entry error:
should "test something" do
Factory.create(:amanda_levy)
assert true
end
should "test something else" do
Factory.create(:amanda_levy)
assert true
end
Error: "ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry"
What gives?

Do you have the the following line in your spec_helper:
config.use_transactional_fixtures = true
That tells rspec/ test::unit to start a transaction at the start
of every test case, and issue a rollback when it's over.

Related

ActiveRecord::QueryMethods not working within rspec test but in console it working fine

I have a test where I want to test that a book_group cannot delete when is associated with a book, this test failed but in the application, this feature works fine
describe 'callbacks' do
context 'before_validation' do
include_examples 'examples for strippable attributes', :book_group, :name, :description, :spawn_button_label
end
it 'is not destroyed and returns a base error if there is an book associated to the it' do
error = 'Cannot delete record because dependent books exist'
book_group.books << create(:book)
expect(book_group.destroy).to be false
end
end
I debugged into the test and found that the error is because this query not working as expected
First, I valid that these two models have an association
pry(#<RSpec::ExampleGroups::bookGroup::Callbacks>)> book_group.books
=> [#<book:0x0000563cb3f6eaf0
id: 1,
review_id: 1,
name: "Vernie Becker",
level: "site",
book_group_id: 831812,
author_book_id: nil]
I do the next query but its result is wrong
book_group.books.where(author_book_id: nil).order(id: :desc).first
=> nil
but this query within console working as expected
[4] pry(main)> #book_group.books.where(author_book_id: nil).order(id: :desc).first DEBUG
[2022-04-02T16:16:49.295Z] book Load (0.7ms) SELECT `books`.* FROM `books` WHERE `books`.`book_group_id` = 6 AND `books`.`author_book_id` IS NULL ORDER BY `books`.`id` DESC LIMIT 1
=> #<book:0x000055d2217339c8 id: 261, review_id: 1, name: "Base book site", level: "site", book_group_id: 6, book_template_id: 2, author_book_id: nil]
the book_group is created in this way
def create
ActiveRecord::Base.transaction do
#book_group = bookGroup.new(book_group_params)
#book_group.author_id = params[:author_id]
#book_group.save!
AllowedActorship.create_from_level_scoped_params(
book_group_params,
#book_group
)
render(
json: { message: #book_group },
status: :ok
)
end
end
I already have reset and prepared the bd, I'm not sure why it working so weird, I will say thank you for whatever helped with it.

Test failing on creating factory

I have a problem concerning testing with factorygirl:
First some code:
customesr_spec.rb:
require 'spec_helper'
describe "customers" do
describe "signup" do
#FactoryGirl.find_definitions
user = FactoryGirl.create(:signup_customer)
it "has right data" do
visit signup_path
fill_in :id, :with => 2110001
fill_in :name, :with => "AVK POLSKA Sp. zo.o."
fill_in :email, :with => "my.email#provider.be"
fill_in :email_confirmation, :with => "my.email#provider.be"
click_button "Create account"
page.should have_content("Fireprotection")
end
end
factories.rb
FactoryGirl.define do
factory :signup_customer, class: Customer do
id = 2110001
name = "AVK POLSKA Sp. zo.o."
email = ""
address_1 = "ul. Jakubowska 1"
address_2 = "Pniewy 62-045"
zipcode = 62
city = "Pniewy"
currency = "PLN"
country_id = "PL"
contact_person_id = "AZU"
reset_token = nil
reset_token_init = nil
end
end
This is the error that I get when running that test:
Running tests with args ["--drb", "-f", "progress", "-r", "c:/Ruby193/lib/ruby/gems/1.9.1/gems/guard-rspec-2.1.1/lib/guard/rspec/formatter.rb",
"-f", "Guard::RSpec::Formatter", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
<-- take tuple(1); slave.run...
09:17:40 - ERROR - Guard::RSpec failed to achieve its <start>, exception was:
[#73C9383A03A6] DRb::DRbUnknownError: ActiveRecord::
[#73C9383A03A6] c:/Ruby193/lib/ruby/1.9.1/drb/drb.rb:1095:in method_missing'
[#73C9383A03A6] c:/Ruby193/lib/ruby/gems/1.9.1/gems/guard-rspec-2.1.1/lib/guard/rspec/runner.rb:124:inrun_via_drb'
Do I have to put a require somewhere? What am I missing here?
First I have to mention this:
I was using Guard with Rspec and Spork on my windows pc.
-> I added Spork to have faster tests once guard and rspec were running.
What I did to solve the problem (with thanks to the freenode #RubyOnRails channel.:
user = FactoryGirl.create(:signup_customer)
#This is wrong! Has to be:
let(:user) {FactoryGirl.create(:signup_customer)}
Did you init the spork? You should have some Spork.prefork blocks in your code so it know what it can keep in memory for the entire time and what it can reload upon each run. I don't see your rspec files here with the spork blocks defined.

Nested attributes in rake task

I'm trying to create a rake task to populate my DB. I need to to create 1 user with a specifc set of deatails and 99 other randomly genraterated . The details that are required are for two Models at the same time, similar to a form with nested attribues. My model uses an after_create call back to create and link a users to a team. The team name by default is the users name
the current code that I have used it producing errors
sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
user = User.create!(:profile_attributes => { name: Forgery::Name.full_name },
email: "test#example.com",
password: "123qwe",
password_confirmation: "123qwe")
99.times do |n|
:profile_attributes => { name: Forgery::Name.full_name },
email = Forgery::Internet.email_address
password = "password"
user = User.create!(email: email,
password: password,
password_confirmation: password)
end
end
end
Model that relys on the nested attibute.
class User < ActiveRecord::Base
after_create :set_user_on_team
private
def set_user_on_team
self.create_owned_team(:name => self.profile.name ).users << self
end
end
error message
rake aborted!
/lib/tasks/sample_date.rake:9: syntax error, unexpected tASSOC, expecting keyword_end
:profile_attributes => { name: Forgery::Name.full_name },
^
/lib/tasks/sample_date.rake:9: syntax error, unexpected ',', expecting keyword_end
I noticed some syntax errors in the 99.times block. You could try it like this:
99.times do |n|
profile_attributes = { name: Forgery::Name.full_name }
email = Forgery::Internet.email_address
password = "password"
user = User.create!(profile_attributes: profile_attributes, #my assumption
email: email,
password: password,
password_confirmation: password)
end

I18n: How to check if a translation key/value pairs is missing?

I am using Ruby on Rails 3.1.0 and the I18n gem. I (am implementing a plugin and) I would like to check at runtime if the I18n is missing a translation key/value pairs and, if so, to use a custom string. That is, I have:
validates :link_url,
:format => {
:with => REGEX,
:message => I18n.t(
'custom_invalid_format',
:scope => 'activerecord.errors.messages'
)
}
If in the .yml file there is not the following code
activerecord:
errors:
messages:
custom_invalid_format: This is the test error message 1
I would like to use the This is the test error message 2. Is it possible? If so, how can I make that?
BTW: For performance reasons, is it advisable to check at runtime if the translation key/value pairs is present?
You could pass a :default parameter to I18n.t:
I18n.t :missing, :default => 'Not here'
# => 'Not here'
You can read more about it here.
I just had the same question and I want to compute an automatic string in case the translation is missing. If I use the :default option I have to compute the automatic string every time even when the translation is not missing. So I searched for another solution.
You can add the option :raise => true or use I18n.translate! instead of I18n.translate. If no translation can be found an exception is raised.
begin
I18n.translate!('this.key.should.be.translated', :raise => true)
rescue I18n::MissingTranslationData
do_some_resource_eating_text_generation_here
end
I don't know how to this at runtime but you can use rake to find it out. You'll have create your own rake task for that. Here's one:
namespace :i18n do
desc "Find and list translation keys that do not exist in all locales"
task :missing_keys => :environment do
def collect_keys(scope, translations)
full_keys = []
translations.to_a.each do |key, translations|
new_scope = scope.dup << key
if translations.is_a?(Hash)
full_keys += collect_keys(new_scope, translations)
else
full_keys << new_scope.join('.')
end
end
return full_keys
end
# Make sure we've loaded the translations
I18n.backend.send(:init_translations)
puts "#{I18n.available_locales.size} #{I18n.available_locales.size == 1 ? 'locale' : 'locales'} available: #{I18n.available_locales.to_sentence}"
# Get all keys from all locales
all_keys = I18n.backend.send(:translations).collect do |check_locale, translations|
collect_keys([], translations).sort
end.flatten.uniq
puts "#{all_keys.size} #{all_keys.size == 1 ? 'unique key' : 'unique keys'} found."
missing_keys = {}
all_keys.each do |key|
I18n.available_locales.each do |locale|
I18n.locale = locale
begin
result = I18n.translate(key, :raise => true)
rescue I18n::MissingInterpolationArgument
# noop
rescue I18n::MissingTranslationData
if missing_keys[key]
missing_keys[key] << locale
else
missing_keys[key] = [locale]
end
end
end
end
puts "#{missing_keys.size} #{missing_keys.size == 1 ? 'key is missing' : 'keys are missing'} from one or more locales:"
missing_keys.keys.sort.each do |key|
puts "'#{key}': Missing from #{missing_keys[key].join(', ')}"
end
end
end
put the given in a .rake file in your lib/tasks directory and execute:
rake i18n:missing_keys
Information source is here and code on github here.
If you wish to pass variable to the message like This is the test error message {variable}
This is possible using variable in language file like below.
# app/views/home/index.html.erb
<%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
# config/locales/en.yml
en:
greet_username: "%{message}, %{user}!"
More description you can find here.

Why is this RSpec test failing?

I'm in the process of learning Ruby on Rails, so treat me like a total neophyte, because I am.
I've got a User model with some associated RSpec tests, and the following test fails:
require 'spec_helper'
describe User do
it 'should require a password' do
User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''}).should_not be_valid
end
end
The relevant part of the User model looks like this:
class User < ActiveRecord::Base
...
validates :password, :presence => true,
:confirmation => true,
:length => { :minimum => 6 }
...
end
Here's the catch: if I run User.new(...).valid? from a Rails console using the arguments above, it returns false as expected and shows the correct errors (password is blank).
I was using spork/autotest and I restarted both to no avail, but this test also fails even running it directly with rspec. What am I doing wrong here?
EDIT
I tried a few more things with the test. This fails:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.should_not be_valid
So does this:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.valid?
u.errors.should_not be_empty
This passes, confirming that :password is indeed blank:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.password.should == ''
So, it's actually spork that is causing the problem. You can turn caching off, so that it won't need restarting every time :
http://ablogaboutcode.com/2011/05/09/spork-testing-tip-caching-classes
I think this is what happens :
ruby-1.9.2-p180 :020 > u = User.new
=> #<User id: nil, email: ...
ruby-1.9.2-p180 :021 > u.errors
=> {}
ruby-1.9.2-p180 :022 > u.save
=> false
ruby-1.9.2-p180 :023 > u.errors
=> {:email=>["can't be blank", "can't be blank"], ...}
In short, if you change new to create, it will work :) I think that this happens because the matcher be_valid checks on the model validation errors. There can be a deeper explanation, but i think that if you use create instead of new, it will work.
EDIT : I have a be_valid_verbose version that might help. Just create a 'be_valid_verbose.rb' file in your rspec/custom_matchers folder, and inside it write :
RSpec::Matchers.define :be_valid_verbose do
match do |model|
model.valid?
end
failure_message_for_should do |model|
"#{model.class} expected to be valid but had errors:n #{model.errors.full_messages.join("n ")}"
end
failure_message_for_should_not do |model|
"#{model.class} expected to have errors, but it did not"
end
description do
"be valid"
end
end
Now check against be_valid_verbose instead of be_valid. It will hopefully present you with some more information on what is happening in your case.
As I feared, the answer was stupidity. This was a spork problem. I thought I had killed the existing process and was running rspec independently, but I later found the spork process still running in a different shell, and rspec had been connecting to it all along. Restarting spork (or killing it entirely) and re-running the tests fixed the problem.
I found this particularly deceptive in that rspec continually updated the test output to reflect the fact that it was aware of my test changes, so it appeared to me that it was running against up-to-date code. Now I'm left to wonder what the real utility of spork is, since apparently I can't trust that it's actually running the right tests correctly.

Resources