Can't use named routes in my rspec feautre tests. - ruby-on-rails

I have the tests that looks like the following:
require "rails_helper"
feature "Ordering units management" do
scenario "Creating new ordering unit" do
visit new_ordering_units_path
fill_in I18n.t('activerecord.attributes.ordering_unit.name'), with: "Ordering unit name"
click_button I18n.t('submit')
expect(page).to have_text(I18n.t('ordering_units.created'))
expect(page).to have_content("Ordering unit name")
end
end
When I run it I have the following error message:
undefined local variable or method `new_ordering_units_path' for #<RSpec::ExampleGroups::OrderingUnitsManagement:0x00000004962810>
My spec_helper:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed do
end
end
My rails helper:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'capybara'
require 'rspec/rails'
require 'capybara/rspec'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_spec_type_from_file_location!
end
I can't find solution for this. Could anyone tell me why this ain't working?

It should be called new_ordering_unit_path (singular). Type rake routes in your console to see all routes in effect.

Related

Rails 5.1: Rspec can't see Capybara features

I have some feature specs in my project that look roughly like this:
# specs/features/canvas_integration.rb
require 'spec_helper'
feature 'Routes to default page' do
let!(:mode) { create(:mode) }
scenario 'as anyone' do
visit root_path
expect(page).to have_content('Your home')
end
end
But running the specs results in nothing:
rspec spec/features
No examples found.
My spec_helper.rb looks like this:
# frozen_string_literal: true
require 'simplecov'
SimpleCov.start 'rails'
require File.expand_path('../../config/environment', __FILE__)
require 'codeclimate-test-reporter'
require 'rspec/rails'
require 'webmock/rspec'
require 'devise'
require 'codeship'
require 'capybara/rspec'
require 'capybara/rails'
WebMock.disable_net_connect!(allow_localhost: true)
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include FactoryGirl::Syntax::Methods
config.extend ControllerMacros, type: :controller
config.include Macros
config.color = true
config.fixture_path = "#{::Rails.root}/spec/factories/fixtures"
config.infer_spec_type_from_file_location!
config.infer_base_class_for_anonymous_controllers = false
config.order = 'random'
config.warnings = false
config.default_formatter = 'doc' if config.files_to_run.one?
config.profile_examples = 10
config.use_transactional_examples = false
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
# config.before(:each, type: :system) { driven_by :rack_test }
config.mock_with :rspec do |mocks|
mocks.verify_doubled_constant_names = true
end
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
Why can't Rspec find my features?
Your files need to end in _spec.rb in order for RSpec to load them when specifying a directory. So rename to specs/features/canvas_integration_spec.rb
Also note that in Rails 5.1 database cleaner is probably not necessary (if you're using one of the usual databases) - so you should be able to remove/comment out that and re-enable transactional testing. Additionally use_transactional_examples is an alias of use_transactional_fixtures so specifying both doesn't make sense.
I think your test spec filename should be specs/features/canvas_integration_spec.rb instead of specs/features/canvas_integration.rb
Why?
The files read by the rspec-rails gem are those that end in _spec.rb

How to configure database_cleaner with rails

I have some services inside my app/services folder.Now I want to test these services, so I am using rspec and factorygirl. I am creating user with factoryGirl, but when i am running rspec it is showing me following error
Failure/Error: #user ||= FactoryGirl.create(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
I am using database_cleaner gem to clean the database after each test.
Below is my config file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factory_girl_rails'
require 'database_cleaner
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::TestHelpers, type: :controller
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
I don't know where i am going wrong.
You need to specify the strategy to use between each specs (transaction is a safe choice 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) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end

undefined method `expect' for #<RSpec::ExampleGroups::TheSigninProcess:0x007fddd28a7750>

I have a very basic RSpec/Capybara Rails test written down but i keep on getting that expect method is undefined
my spec/features/signin_spec.rb:
require "rails_helper"
RSpec.feature "the signin process", :type => :feature do
before :each do
User.create(:email => "user#example.com", :password => "password")
end
scenario "signs me in" do
visit "/users/sign_in"
within("#new_user") do
fill_in "Email", :with => "user#example.com"
fill_in "Password", :with => "password"
end
click_button "Log in"
expect(page).to have_content "successfully"
end
end
rails_helper:
This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
spec_helper:
require 'capybara/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
config.expect_with(:rspec) { |c| c.syntax = :should }
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
=begin
config.filter_run :focus
config.run_all_when_everything_filtered = true
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
=end
end
Adding :expect to this in spec_helper removed the error
config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end
https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration#disable-expect-syntax
Thank you for your help #apneadiving and Frederick
Just replace
expectations.syntax = :should
with
expectations.syntax = [:should , :expect]
in your spec_helper.rb
just replace
expectations.syntax = :should
with
expectations.syntax = [:should , :expect]
In your spec_helper.rb

Problems with rspec 3, capybara and machinist 2

I have this feature test (Store model have a uniqueness validation):
feature "Index" do
before do
3.times { Store.make! }
end
scenario "User visit index" do
visit stores_path
within("table.table") do
Store.all.each do |store|
expect(page).to have_content(store.name)
end
end
end
end
When i run the test, randomly fails:
Failure/Error: 3.times { Store.make! }
ActiveRecord::RecordInvalid:
Validation failed: name has already been taken
My blueprint is (follow the machinist documentation to generate a unique record):
Store.blueprint do
name { "store #{sn}" }
end
I use DatabaseCleaner gem, then my config rails_spec:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'pundit/rspec'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before(:each) do |example|
DatabaseCleaner.strategy = if example.metadata[:js]
:truncation
else
:transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
DatabaseCleaner.logger = Rails.logger
config.infer_spec_type_from_file_location!
That errors are driving me crazy
Try to replace the following in your spec/spec_helper.rb:
config.before :suite do |example|
DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
end
config.before do
DatabaseCleaner.clean
end

FactoryGirl objects not being dropped after tests. Please review my test setup

Objects are persisting after tests run. I confirmed by doing a PowerUp.all.count test and the count increases by 2 on each run, which a number equal to the objects created for the test on each run. I don't know if I'm misusing FactoryGirl, or if I have a misconfigured spec_helper.
spec/support/factory_girl.rb:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/rails_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
end
spec/spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
RSpec.configure do |config|
config.order = :random
config.before(:all) do
FactoryGirl.reload
end
Kernel.srand config.seed
config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
config.mock_with :rspec do |mocks|
mocks.syntax = :expect
mocks.verify_partial_doubles = true
end
end
spec/api/power_up_spec.rb:
describe Api::PowerUpsController, :type => :controller do
describe "GET power_ups" do
it "returns all power-ups" do
FactoryGirl.create :power_up, name: "Increase rate of take", description: "You gain points more quickly"
FactoryGirl.create :power_up, name: "Decrease rate of give", description: "You lose points more slowly"
get :index, :format => :json
expect(response.status).to eq 200
body = JSON.parse(response.body)
power_up_names = body.map { |m| m["name"] }
expect(power_up_names).to match_array(["Increase rate of take",
"Decrease rate of give"])
end
end
end
config.use_transactional_fixtures = false
This turns off the default behavior, which is to rollback transactions after each example. When set to false RSpec does not attempt to manage the test database.
You can use database_cleaner to remove the rows created by tests when RSpec is not using transactions. This is often used when writing feature specs.

Resources