Setting FactoryGirl and use it in Grape and RSpec? - ruby

In my Grape ruby project. I'm separating my models to a gem so I can use it between my ruby projects.
Now the problem is that with my activerecords, let's say I'm dealing with User model, now it looks something like this:
module MyApp
module Core
class User < ActiveRecord::Base
self.table_name = 'users'
end
end
end
And I'm using Factory girl something like this:
FactoryGirl.define do
factory :user do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password { "12345678" }
password_confirmation { "12345678" }
end
end
And let's say I have the following rspec test:
require 'spec_helper'
describe MyApp::Core::User do
it "has name assigned" do
user = build(:member, first_name: 'Eki', last_name: 'Eqbal')
expect(user.first_name).to eq('Eki')
expect(user.last_name).to eq('Eqbal')
end
end
And when I try to run the test:
⇒ bundle exec rspec spec/unit/users_spec.rb
warning: parser/current is loading parser/ruby22, which recognizes
warning: 2.2.3-compliant syntax, but you are running 2.2.0.
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
F
Failures:
1) MyApp::Core::User has name assigned
Failure/Error: user = build(:user, first_name: 'Eki', last_name: 'Eqbal')
NameError:
uninitialized constant User
# ./spec/unit/users_spec.rb:37:in `block (2 levels) in <top (required)>'
Finished in 0.14788 seconds
1 example, 1 failure
Failed examples:

Instead of:
factory :user do
Try:
factory :user, class: MyApp::Core::User do
FactoryGirl guesses the class name based on the factory name, so if it's in a module like that, it won't find it.

Related

How to test class methods that rely on associations with RSpec and Sinatra?

I've written some RSpec tests that successfully create objects with :let statements. However, the test environment doesn't maintain the associations that function properly everywhere else. Below is an example of a class that would turn up a NoMethodError (undefined method `money' for nil:NilClass). Money is a column in Inventory. Any thoughts?
class Inventory < ActiveRecord::Base
belongs_to :character
def self.return_money(character)
character.inventory.money
end
end
And here's a corresponding example for a spec doc:
require 'spec_helper'
describe 'Test methods' do
let(:trader) {
Character.create(
name: "Trader",
location_id: 1)
}
let(:trader_inventory) {
Inventory.create(
character_id: trader.id,
storage_capacity: 50000,
money: 20000,
markup: 1.35)
}
it "test method" do
expect(Inventory.return_money(trader)).to eq(100)
end
end
There is no reason this shouldn't work. RSpec isn't special, it's just regular Ruby code. You can confirm this by moving all of your code into a single file, something like:
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
class Inventory < ActiveRecord::Base
end
class Character < ActiveRecord::Base
has_one :inventory
end
describe 'test' do
it 'works' do
puts Character.first.inventory.money.inspect
end
end
Guesses as to what may be broken:
money is a composite field or something like that. Can you post your database schema?
Library files aren't being loaded correctly. Use puts $LOADED_FEATURES to verify that all the files that should be required have been.

active record with ruby (not rails)

I am using active record with ruby (but not rails). I am using sqlite3 which has a test.db on file (not just in-memory). When I run the following code snippet using user.create, it complains about argument error (and when I use use.save, it throws an active record exception. Any idea what I might be doing wrong? Thanks
require 'rubygems'
gem 'activerecord'
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
#ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:host => "localhost",
:database => 'test.db'
)
class User < ActiveRecord::Base
#attr_accessible :email, :full_name
attr_accessor :email
attr_accessor :full_name
validates :email, presence: true, uniqueness: true
def initialize(email, full_name)
#email = email
#full_name = full_name
end
end
puts "full_name for user:"
full_name = gets.chomp
puts "email address:"
email = gets.chomp
user = User.new(email, full_name)
#user.save
user = User.create!(email: '', full_name: '')
Exception in first case (with User.create!):
main.rb:42:in `initialize': wrong number of arguments (1 for 2) (ArgumentError)
from /var/lib/gems/1.9.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:61:in `new'
from /var/lib/gems/1.9.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:61:in `new'
from /var/lib/gems/1.9.1/gems/activerecord-4.2.4/lib/active_record/persistence.rb:50:in `create!'
from main.rb:55:in `<main>'
It is complaining about the new method. According to the documentation: (http://api.rubyonrails.org/classes/ActiveRecord/Base.html), you don't need the initialize, because when you inherit from ActiveRecord::Base, you need to initialize your objects with a hash.
user = User.new({email: email, full_name: full_name})
# or
user = User.new(email: email, full_name: full_name)
# then
user.save
You need to drop the initialize and the attr_accessor from your code.
Try to comment User#initialize method and create new user like this:
User.create! email: 'halk#mail.com', full_name: 'Halk'
Explanation
When you declare AR model by heritage from ActiveRecord::Base class you don't need to define your own #initialize method. But you do. When you call User::create! method, you pass only one argument - Hash with two pairs (with email and full_name keys). But User#initialize define two parameters - email and full_name separately. So Ruby exception raise and talk about it:
wrong number of arguments (1 for 2) (ArgumentError)

Ruby basic RSpec test does not pass

I'm not able to understand why the following Rspec test does not pass -
require "rspec"
require_relative "file-to-be-tested"
describe Customer do
it "is valid with firstname" do
customer = Customer.new("handy")
expect(customer).to be_valid
end
end
for the corresponding Class definition -
class Customer
attr_reader :firstname
def initialize(firstname)
#firstname = firstname
end
end
these two code snippets are in separate files in the same folder, so when i run ~rspec <first-filename> in the terminal, I get the following error -
F
Failures:
1) Customer is valid with firstname
Failure/Error: expect(customer).to be_valid
expected #<Customer:0x007f90e50f3110> to respond to `valid?`
# ./poodr/poodr_rspec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 0.00551 seconds (files took 0.52876 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./poodr/poodr_rspec.rb:6 # Customer is valid with firstname
be_valid is an rspec-rails method, but it looks like you're using just straight rspec. you could do something like:
require "rspec"
require_relative "file-to-be-tested"
describe Customer do
it "is valid with firstname" do
expect { Customer.new('handy') }.to_not raise_error
end
end
What are you expecting the to be_valid test to do? The issue is that the Customer class has no method called valid? which your test is trying to test.
A hack to move your test along if your doing test driven development:
class Customer
def valid?
true
end
end
You now have a method called valid and your test will pass. Obviously it shouldn't always be true so your next step would be to expand the definition of valid?. What check needs to be done to know if a customer is valid or not?

Rspec + Factory girl (without rails!)

I am using Rspec with selenium-webdriver gem to test a web app. And I wanted to unclude factories in my tests to emulate users and not to create a user manually each time.
So, I made gem install factory_girl, added required lined in my spec_helper, created a factory and included some lines in my spec file. And when running the test I get an error
Failure/Error: FactoryGirl.build(:user)
NameError:
uninitialized constant User
Here is my spec_helper.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
My factories.rb file:
FactoryGirl.define do
factory :user do
name "testuser"
password "freestyle"
inventory true
end
end
And my test_spec file:
require "json"
require "selenium-webdriver"
require "rspec"
require "factory_girl"
FactoryGirl.find_definitions
include RSpec::Expectations
describe "MallSpec" do
before(:all) do
FactoryGirl.build(:user)
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://localhost:9000/"
#accept_next_alert = true
#driver.manage.timeouts.implicit_wait = 30
#driver.manage.window.resize_to(1301, 744)
#verification_errors = []
end
My spec_file is in the root dir of the project. my factories.rb file is in /spec dir as well as the test_spec.rb itself.
Can anyone help me with this issue or point what i am doing wrong?
If you don't actually have a User class but you want to use FactoryGirl to generate the attributes, you can override the class:
require "ostruct"
FactoryGirl.define do
factory :user, class: OpenStruct do
name "testuser"
password "freestyle"
inventory true
# This isn't necessary, but it will prevent FactoryGirl from trying
# to call #save on the built instance.
to_create {}
end
end
You can then use attributes_for if you just want a Hash, or create if you want an object that responds to methods like name.
You can use a library like Hashie::Mash if you want to generate JSON for use in your API:
factory :user, class: Hashie::Mash do
# ...
end
# In your tests:
user_json = create(:user).to_json
And when running the test I get an error Failure/Error:
FactoryGirl.build(:user) NameError: uninitialized constant User
Your User class has to be defined. The following is a test with no User class defined:
require 'factory_girl'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
FactoryGirl.define do
factory :user do
name 'Alice'
age 10
end
end
describe "MallSpec" do
let(:test_user) { FactoryGirl.build(:user) }
describe "user's name" do
it "equals 'Alice'" do
expect(test_user.name).to eq('Alice')
end
end
end
--output:--
$ rspec 1.rb
F
Failures:
1) MallSpec user's name equals 'Alice'
Failure/Error: let(:user) { FactoryGirl.build(:user) }
NameError:
uninitialized constant User
...
Adding a definition for the User class:
require 'factory_girl'
#====NEW CODE=====
class User
attr_accessor :name, :age
end
#=================
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
FactoryGirl.define do
factory :user do
name 'Alice'
age 10
end
end
describe "MallSpec" do
let(:test_user) { FactoryGirl.build(:user) }
describe "user's name" do
it "equals 'Alice'" do
expect(test_user.name).to eq('Alice')
end
end
end
--output:--
$ rspec 1.rb
.
Finished in 0.0024 seconds (files took 0.35197 seconds to load)
1 example, 0 failures
I expect that the factory() method here:
factory :user do
name 'Alice'
age 10
end
...does something like this:
def factory(model_name)
target_class = constant_get(model_name.capitalize)
...in order to construct a real instance of the User class. In other words, factory_girl constructs instances of classes that already exist in your app--factory_girl does not mock a class.

FactoryGirl association Parent can't be blank

I am trying to use FactoryGirl to create a test database with an association that is Parent has_many Entries. At this point, it is throwing the ActiveRecord validation error that Parent can't be blank. I'm having a tough time with this one and have tried many, many methods of creating this test database with this association. I think I am close, but I may not be even close and may have basic errors, so any and all advice is much appreciated.
My guess is that the hash { parent_id: :id } is not being passed to the Entry factory. That would fail the validation. But, I don't know that that is actually the case and, even if it is, I don't know how to fix it. Thanks in advance for your help...
The error is:
ActiveRecord::RecordInvalid: Validation failed: Parent can't be blank
The RSpec call is:
before(:all) do
rand(11..25).times { FactoryGirl.create(:parent) }
visit "/parents?direction=asc&sort=parent_blog"
end
after(:all) do
Parent.delete_all
end
The Parent model is:
class Parent < ActiveRecord::Base
has_many :entries, dependent: :destroy
accepts_nested_attributes_for :entries, :allow_destroy => :true
validates :parent_blog, presence: true,
uniqueness: true
end
The Entry model is:
class Entry < ActiveRecord::Base
belongs_to :parent
validates :entry_blog, presence:true,
uniqueness: true
validates :parent_id, presence: true
end
The Parent factory is:
FactoryGirl.define do
factory :parent do
sequence(:parent_blog) { |n| "http://www.parent%.3d.com/ss" % n }
entries { rand(5..15).times { FactoryGirl.create(:entry, parent_id: :id) } }
end
end
The Entry factory is:
FactoryGirl.define do
factory :entry do
sequence(:entry_blog) { |n| "http://www.parent%.3d.com/ss/entry%.3d" % [n, n] }
parent_id { :parent_id }
end
end
The following modification to your factory definitions should work. I'll be back later to offer some explanation.
FactoryGirl.define do
factory :parent do
sequence(:parent_blog) { |n| "http://www.parent%.3d.com/ss" % n }
after(:create) {|parent| rand(5..15).times {FactoryGirl.create(:entry, parent: parent)}}
end
factory :entry do
sequence(:entry_blog) { |n| "http://www.parent%.3d.com/ss/entry%.3d" % [n, n] }
parent
end
end
The two changes, introducing the use of after in the :parent factory and using parent instead of parent_id, are both examples of RSpec's support of associations, as discussed in https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations.
As for why your code didn't work, I can only provide a partial answer at this point. The reason you can't include entries as part of the initial parent creation is that you don't have the parent id until the parent record is created, yet you need the parent id to create the entries because of Entry validating the presence of parent_id. Put another way, parent_id hadn't been set when the entries block was evaluated in your parent factory.
What I'm not sure of is why you can't replace parent with parent_id in the entry factory and correspondingly replace parent: parent with parent_id: parent.id in the FactoryGirl.create call in the parent factory. I tried that variant before submitting the above and it failed with:
1) test
Failure/Error: rand(11..25).times { FactoryGirl.create(:parent) }
ArgumentError:
Trait not registered: parent_id
# ./spec/factories.rb:4:in `block (4 levels) in <top (required)>'
# ./spec/factories.rb:4:in `times'
# ./spec/factories.rb:4:in `block (3 levels) in <top (required)>'
# ./tmp/factory_spec.rb:5:in `block (3 levels) in <top (required)>'
# ./tmp/factory_spec.rb:5:in `times'
# ./tmp/factory_spec.rb:5:in `block (2 levels) in <top (required)>'
I'll update this answer again if/when I figure that out.
I ran into this issue when I upgraded from factory bot 4.11 to 6.2. I suspect this is because a factory bot 5 change:
Changed: use_parent_strategy now defaults to true, so by default the build strategy will build, rather than create associations
Here's what my parent and child factories look like:
factory :parent do
sequence(:name) { |n| "Parent #{n}" }
end
factory :child do
sequence(:name) { |n| "Child #{n}" }
parent
end
I had to change the way I was defining my variables in the spec file.
Instead of:
let(:parent){ create(:parent) }
let(:child) { build(:department) }
Used this:
let(:parent){ create(:parent) }
let(:child) { build(:department, parent: parent) }

Resources