Rails Engine with RSpec and FactoryGirl - ruby-on-rails

I followed a few tutorials and docs of FactoryGirl to use with RSpec. Currently I get one error when trying to use FactoryGirl.create:
describe "GenericRecipesController" do
describe "GET 'index'" do
it "displays list of generic recipes" do
generic_recipe = FactoryGirl.create(:generic_recipe)
visit '/recipe'
response.should be_success
end
end
end
And the error:
GenericRecipesController GET 'index' displays list of generic recipes
Failure/Error: generic_recipe = FactoryGirl.create(:generic_recipe)
NameError:
uninitialized constant GenericRecipe
# ./spec/integration/generic_recipes_spec.rb:8:in `block (3 levels) in <top (required)>'
The rest of code is there.

You can try this:
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
# ...
end
I think problem in a nesting model in module
Upd: delete file /spec/factories.rb, in file /spec/support/factories.rb make
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
When you will run tests, probably will see 'can not load table'. Make
rake db:migrate RAILS_ENV=test
and try again.

You don't seem to have a GenericRecipe model in your app. Factory Girl is looking for a Model called GenericReciper and can't find it.

Related

Factory Girl undefined method for nil:NilClass

I have a controller spec something like this
describe :bizzaro_controller do
let(:credit_card_account) { FactoryGirl.build :credit_card_account }
it "doesn't blow up with just the stub" do
CreditCardAccount.stub(:new).and_return(credit_card_account)
end
it "doesn't blow up" do
credit_card_account
CreditCardAccount.stub(:new).and_return(credit_card_account)
end
end
Which results in this:
bizzaro_controller
doesn't blow up with just the stub (FAILED - 1)
doesn't blow up
Failures:
1) bizzaro_controller doesn't blow up
Failure/Error: let(:credit_card_account) { FactoryGirl.build :credit_card_account }
NoMethodError:
undefined method `exp_month=' for nil:NilClass
# ./spec/controllers/user/bizzareo_controller_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/controllers/user/bizzareo_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.23631 seconds
2 examples, 1 failure
My credit card factory looks like this:
FactoryGirl.define do
factory :credit_card_account do
exp_month 10
exp_year 2075
number '3'
end
end
My CreditCardAccount is an empty ActiveRecord::Base model
=> CreditCardAccount(id: integer, exp_month: integer, exp_year: integer, number: string)
Versions
0 HAL:0 work/complex_finance % bundle show rails rspec-rails factory_girl
/home/brundage/.rvm/gems/ruby-2.0.0-p247#complex_finance/gems/rails-4.0.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247#complex_finance/gems/rspec-rails-2.14.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247#complex_finance/gems/factory_girl-4.2.0
This should be working. all points that your test database is not correct.
RAILS_ENV=test rake db:drop db:create will drop and recreate your test database. Then try to run your rspec using the rake command, in order to migrate the database: rake rspec
I was having the same problem, but I think the cause of my problem was different. My solution, however, may perhaps be useful: I used the Fabrication gem (http://www.fabricationgem.org/) instead of FG.
The reason why I was having this problem was because I was trying to have FG create/build an object that was not ActiveRecord, it was only an ActiveModel, and it had to be initialized with arguments.
I didn't see in the Fabricator documentation an example totally like what I needed, but I got it with this syntax:
Fabricator(:my_class) do
on_init do
init_with("Company Name", "Fake second arg")
end
end
My problem was that in model I made a private method called :send (forgot that it is already used in Ruby).

NameError: uninitialized constant Factory

I'm following this tutorial to get started with TDD on rails with factory girl, rspec and i ran into this issue i can't get my head around.
Here's my "factory".rb (events.rb)
require 'faker'
FactoryGirl.define do
factory :event do
name "HIGH"
genre "house, techno, idb"
venue_name "Westbourne Studios"
venue_address "4-6 Chamberlayne Road"
venue_postcode "NW103JD"
begin_time "10pm"
end_time "2am"
user_id 2
description "A Super massive party with loads of everything you would want around."
status true
venue_id nil
end
end
and here's the event_spec.rb:
require 'spec_helper'
require 'factory_girl_rails'
describe Event do
it "has a valid factory" do
Factory.create(:event).should be_valid
end
it "is invalid without a name"
it "is invalid without a genre"
it "is invalid without a venue_name"
it "is invalid without a venue_address"
it "is invalid without a venue_postcode"
...
end
I have setup the model, migrated etc.. and when i run "rspec spec/models/event_spec.rb" i get the following error:
Failures:
1) Event has a valid factory
Failure/Error: Factory.create(:event).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/event_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.1682 seconds
13 examples, 1 failure, 12 pending
Failed examples:
rspec ./spec/models/event_spec.rb:6 # Event has a valid factory
Randomized with seed 64582
Try to use it in this way:
FactoryGirl.create(:event).should be_valid
I think, I can remember, that it was only "Factory" in old versions of the gem. If you take a look in the recent "Getting started" guide of Factory Girl, there are only calls with "FactoryGirl".
If you create file spec/support/factory_girl.rb with content:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Then you can simply use:
create(:event)
build(:book)
Instead of:
FactogyGirl.create(:event)
FactogyGirl.build(:book)
I had the same error with factory bot, and to supplement Stefan's answer I came across this little cheat sheet.
https://devhints.io/factory_bot

Capybara NoMethodError: undefined method `visit' for

Thank for response.
I getting error in application Git application with the error
Failures:
1) Page pages page creation with invalid information should not create a page
Failure/Error: before(:each) { visit new_admin_page_path }
NoMethodError:
undefined method `visit' for #<Page:0x00000005094bb0>
# ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'
2) Page pages page creation with invalid information error messages
Failure/Error: before(:each) { visit new_admin_page_path }
NoMethodError:
undefined method `visit' for #<Page:0x000000051671f0>
# ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 1.21 seconds
14 examples, 2 failures
What I've missed ?
I cloned your repository and ran the spec. The problem is that you are using the term page for two different things, the Capybara page (subject { page }) and your model with the name Page (which you also name page). Those two names conflict, which is why rspec is complaining that there is no method visit for #<Page:0x00000005094bb0>.
To solve this just rename the page you create in the initial let:
let(:mypage) { Factory.create(:page) }
That will avoid the conflict by naming your page model mypage instead of page.
There are other issues however. Your user factory does not use a sequence for assigning emails, so you get a validation error because it says the email is already taken.
I changed your user factory to this:
sequence(:email) { |n| "abc#{n}#example.com"}
factory :user do
email
password "foobar"
password_confirmation "foobar"
...
That fixes the validation error, but you now get another error. In the line in your first spec, you should be checking that count does not change on the class Page, not the record page (or mypage).
So change that to:
expect { click_button "Create Page" }.not_to change(Page, :count)
This will still leave you with errors, because you also do not have any create action for your PagesController. From what you wrote, it seems like the information is invalid so it should not even get to this action. I don't know exactly what you're trying to do here so I'll leave the rest for you to figure out.
If you recently upgraded to Capybara 2.0 & your integration tests were working with previous version of Capybara, you may try renaming requests directory to features. Check http://www.codelearn.org/blog/how-to-make-old-integration-tests-work-with-capybara-2-0
Add the following to your spec/spec_helper.rb (in your Spork.prefork block):
require 'capybara/rspec'
require 'capybara/rails'

Shoulda for Rspec, Rails, outputs there is an error but doesn't output the error itself

Well, I'm doing some testing right with Rails+Rspec+Shoulda.
When I do a test like the following:
context #user do
describe 'Validation' do
describe :name
it { should allow_value('something').for :name }
end
end
end
When it fails, Rspec just output:
1) Validation name Valid
Failure/Error: it { should allow_value(value).for :name }
Did not expect errors when name is set to "something", got error:
# ./spec/models/user_spec.rb:4:in `block (3 levels) in <top (required)>'
It even says got error: but it doesn't output it! I actually know there is a validation error there, but I want Rspec to tell me that, how I would know what is failing to validate then?
What am I doing wrong? Is that the expected behaviour? I have to overwrite the helpers?
I dug into the Shoulda code and I found that it doesn't show the errors when checking for positive assert. But them are loaded into the #errors variable. So I just monkey patched the one method that defines the output:
module Shoulda
module ActiveRecord
module Matchers
def failure_message
"Did not expect #{expectation}, got error: \n#{#expected_message ? #matched_error : #errors.join("\n ")}"
end
end
end
end
The original said:
"Did not expect #{expectation}, got error: #{#matched_error}"
I saved it to /lib/shoulda/activerecord/matchers.rb and loaded it with config.autoload_paths += Dir["#{config.root}/lib/**/"]
Hope this helps someone with the same issue ^^
Yup welcome to spec testing so you need to recreate the error in console if you want the error, rspec is not a debugger just a test suite.
I run into this a lot

RSpec fails for no obvious reason

So I'm using rspec to test my code as I'm going through the Rails Tutorial, and I keep getting this error when I test the code in listing 3.20. Everything checks out when I look at it with my eyeball, but RSpec doesn't seem to like it.
(Note that I just did one of the pages, not all three because they all give the same error)
james#tristan:~/rails_projects/sample_app$
rspec
spec/controllers/pages_controller_spec.rb
F...
Failures:
1) PagesController should have the
right title
Failure/Error: response.should have_selector("title",
expected following output to contain a | Home tag:
# ./spec/controllers/pages_controller_spec.rb:13:in
`block (2 levels) in '
Finished in 0.97999 seconds 4
examples, 1 failure
james#tristan:~/rails_projects/sample_app$
At the top of that spec file it says:
before(:each) do
#
# Define #base_title here.
#
end
Does your spec assign a value to #base_title?

Resources