I have set up Rspec with a rails4 app however the tests are returning:
Failure/Error: user = Factory(:user)
NoMethodError:
undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa08c0d8a98>
Seems like I'm including FactoryGirl incorrectly. I've tried a couple of variations but I can't get it to work.
My Spec Helper:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL, :type => :request
config.include FactoryGirl::Syntax::Methods
end
gem file:
group :development, :test do
gem 'rspec-rails', '~> 2.0'
gem 'factory_girl_rails', :require => false # as per sugestion on SO. Without the require false also fails
gem "capybara"
end
my spec:
require 'spec_helper'
describe "Create Event" do
describe "Log in and create an event" do
it "Allows creation of individual events" do
user = Factory(:user)
visit "/"
end
end
end
and in /spec/factories/users.rb
FactoryGirl.define do
factory :user do |f|
f.email "provider#example.com"
f.password "password"
end
end
How can i get going with factory girl?
Given that you have included:
config.include FactoryGirl::Syntax::Methods
In your RSpec configure block, I'm guessing that the syntax you're looking for is:
user = create(:user)
or:
user = build(:user)
To create a user using FactoryGirl:
user = FactoryGirl.create(:user)
This will use everything you defined in the factory to create the user. You can also override any value, or specify additional values at the same time. For example:
user = FactoryGirl.create(:user, username: 'username', email: 'different-email#example.com)
Alternatively you can use FactoryGirl.build(:user, ...) where you want to make use of the factory to build an instance but not actually save it to the database.
Related
I am just getting started on rspec, and I may have included to much in the first go. I followed this guide but cant seem to get it all to come together.
I have a Model called Photo, so I have created a Factory girl for that:
# spec/factories/photo.rb
FactoryGirl.define do
factory :photo do
date_taken { Faker::Date.backward(14) }
end
end
I use this in a test:
#spec/models/photo_spec.rb
require 'rails_helper'
RSpec.describe Photo, type: :model do
it "has a valid factory" do
expect(photo).to be_valid
end
end
But I get the following, indicating that the photo factory is not loaded...
rspec spec/models/photo_spec.rb
Failures:
1) Photo has a valid factory
Failure/Error: expect(photo).to be_valid
NameError:
undefined local variable or method `photo' for #<RSpec::ExampleGroups::Photo:0x00000004b769f8>
# ./spec/models/photo_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.29491 seconds (files took 7.67 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/photo_spec.rb:4 # Photo has a valid factory
My config is:
#spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'shoulda/matchers'
require 'database_cleaner'
require 'support/factory_girl'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
in support dir:
#spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
#spec/support/database_cleaner.rb`enter code here`
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Down in the comments section on the linked guide, there's a comment by Rhys Davis on 11 Jan 2016
Although in the Model Specs section I had to add the following to
rails_helper.rb: config.include FactoryGirl::Syntax::Methods
then also add the following to my contact_spec.rb: contact =
build(:contact)
and then a reply by Arkadiusz Zdanowski on 5 Sep
Yes, without those lines I got the following error:
Failures:
1) Contact has a valid factory
Failure/Error: expect(contact).to be_valid
NameError:
undefined local variable or method `contact' for #
Which is the same error you're getting now, so adding those 2 lines should probably fix the issue for you.
My problem is that capybara holds only one authorized request (doesn't matter was it authorization through the login form or through the helper login_as method)
Other (second, third) requests are unauthorized!
It doesn't mater what driver I use. Where the problem can be?
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
# NOTE: For Chrome support
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
# Capybara.register_driver(:poltergeist) { |app| Capybara::Poltergeist::Driver.new(app, js_errors: false) }
# Capybara.javascript_driver = :poltergeist
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include WaitForAjax
config.include FeatureHelpers
config.include Devise::TestHelpers, type: :controller
config.include Warden::Test::Helpers
config.extend ControllerHelpers, type: :controller
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.before :suite do
Warden.test_mode!
end
end
Most probably between the first request and the second, rspec cleans up the DB.
For this second request the user form the first request no more exists. You should create another user and login/authorize it.
Ex:
For each it block you'll have to do a login_as(a_new_just_created_user). Yes, you can put this into a before block to be executed for the entire test suite.
describe '' do
before do
login_as(a_new_just_created_user)
end
context '' do
it '' { get 'api/url' } # first user logged in
it '' { get 'api/url' } # second user logged in
end
context '' do
it '' { get 'api/url' } # third user logged in
end
end
I have a simple rails app which Im trying to introduce elementary tests in. Im following this url.
My application is structured as follows:
Im trying to test the single page: called Main Page.
In spec/features/main_pages_spec.rb I have the following code:
require 'spec_helper'
feature "soadevise" do
feature "Main Page" do
scenario "should have the content 'Main Page' "
visit '/main_page/home'
expect(page).to have_content('Main Page')
end
end
My spec/spec_helper.rb looks like this:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.include Capybara::DSL
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
When I run this command in the terminal:
MRMIOMP0903:soadevise $ bundle exec rspec spec/features/main_pages_spec.rb
I get the following error:
/Users/am/Desktop/x/xx/rails_projects/mysql_apps/soadevise/
spec/features/main_pages_spec.rb:6:
in `block (2 levels) in <top (required)>': undefined method `visit' for #
<Class:0x007fb7237377b0> (NoMethodError)
from /Users/am/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core 2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval'
from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass'
from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe'
My gemfile contains these:
#For testing
group :development, :test do
gem "rspec-rails", "~> 2.14.1"
end
group :test do
gem "selenium-webdriver"
gem "capybara"
end
I refered to this link as well.
Can someone suggest what Im doing wrong?
You're calling feature twice. It should look like this:
require 'spec_helper'
feature "soadevise Main Page" do
scenario "should have the content 'Main Page'" do
visit '/main_page/home'
expect(page).to have_content('Main Page')
end
end
I want to test a model with RSpec but I probably have stumbled on a typo that I just can't find. Can somebody please help me a bit? I've been struggling with it for a long time and just can't find any mistakes. Thank you in advance!
user_spec.rb
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
it "is invalid without an e-mail"
it "is invalid without a correct e-mail"
it "is invalid without a password"
it "is invalid without a matching password confrimation"
end
user.rb
FactoryGirl.define do
factory :user do |f|
f.email "aabb#hh.de"
f.password "ruby"
f.password_confrimation "ruby"
end
end
spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
error
Factory not registered: user
You have your factory definition in the wrong file, according to your question it is in user.rb. This needs to be in a factories.rb in your test folder (spec) if you use rspec
# user.rb
FactoryGirl.define do
factory :user do |f|
f.email "aabb#hh.de"
f.password "ruby"
f.password_confrimation "ruby"
end
end
Change above to this, (Also you don't need the f variable)
# spec/factories.rb
FactoryGirl.define do
factory :user do
email "aabb#hh.de"
password "ruby"
password_confrimation "ruby"
end
end
Also as the comments say, make sure gem 'factory_girl_rails' is in your Gemfile, instead of just gem 'factory_girl'
I had run into the following issue with the structure in my folder factories/
- factories/
-- artists.rb
-- techniques.rb
artists.rb
FactoryBot.define do
factory :artist do
name 'Michael'
technique FactoryBot.create :technique
end
end
techniques.rb
FactoryBot.define do
factory :technique do
name 'Some name'
end
end
So it was loading artist before technique object were loaded. So it couldn't find it. The solution is to not use nested FactoryBot create in factories or rename your nested factories to something that stands before your parent factory.
I just moved my technique factory. to factories.rb and defined it there. And issue was resolved.
I got this error when running the Rails application in the production environment.
Usually, the Gem factory_bot_rails is only added here:
group :development, :test do
gem 'factory_bot_rails'
gem 'faker'
end
You should add these to the "general" list of Gems if you want to use them in production.
I had the same issue and the only thing I added was a factories.rb file. All tests passed from that.
When running the rspec I get:
C:/www/kill/spec/games_controller_spec.rb:4:in block in <top
(required)>': undefined local variable or methodrender_views'
games_controller_spec.rb
require 'spec_helper'
describe GamesController, "creating a new game" do
render_views
fixtures :games
it "should redirect to index with a notice on successful save" do
Game.any_instance.stubs(:valid?).returns(true)
post 'create'
assigns[:game].should_not be_new_record
flash[:notice].should_not be_nil
response.should redirect_to(menu_items_path)
end
end
spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
#config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
end
gems:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-2.7.0
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mocha-0.10.0
Your controller specs should be in the spec/controllers/ folder. However, you may want for some reason to have some other files that run controller specs (for example, I'm testing markup validation in a separate test file).
You can do this :
describe GamesController, "creating a new game", :type => :controller do
And this should do the trick !
You should place your controller specs inside a spec/controllers directory. RSpec is including these methods based on this, AFAIK.
In my case my test was already in spec/controllers/. I needed to add config.infer_spec_type_from_file_location! inside the RSpec.configure do |config| loop in spec_helper.rb. I had updated rspec from 2.x to 3.x without realizing it, and that config is needed for controller tests from 3.x.
If you test the views, RSpec have the spec/requests directory for that, it will render views by default. (See the doc)
this problem may be caused by your Gem rspec-rails version. downgrade it back to 2.x solved my problem:
# edit your Gemfile
gem "rspec-rails", "2.14.2"