How do I simulate a login with RSpec?

The answer depends on your authentication implementation. Normally, when a user logs in, you’ll set a session variable to remember that user, something like session[:user_id]. Your controllers will check for a login in a before_filter and redirect if no such session variable exists. I assume you’re already doing something like this. To get this working … Read more

Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

The gem factory_girl_rails should be required in the spec_helper.rb rather than the gemfile – it is possible that you are requiring FactoryGirl twice which is why you are getting the duplicate. Try this in your gem file: group :test do gem “rspec” gem ‘factory_girl_rails’, :require => false end Then make sure that factory girl is … Read more

“undefined method `env’ for nil:NilClass” in ‘setup_controller_for_warden’ error when testing Devise using Rspec

In Rails 5 you must include Devise::Test::IntegrationHelpers instead Devise::Test::ControllerHelpers: # rails_helper.rb config.include Devise::Test::IntegrationHelpers, type: :feature See more: https://github.com/plataformatec/devise/issues/3913#issuecomment https://github.com/plataformatec/devise/pull/4071

ActiveModel::MissingAttributeError: can’t write unknown attribute `ad_id’ with FactoryGirl

When you say: has_one :image Rails expects you to define an ad_id field at the images table. Given the way your associations are organised, I assume you have an image_id and a logo_id a the ads table so instead of: class Ad < ActiveRecord::Base belongs_to :page has_one :image has_one :logo end You probably mean: class … Read more

Can FactoryBot generate factories after your models have been created?

First thing, look at the source project to find out how it was implemented: https://github.com/thoughtbot/factory_bot_rails/blob/master/lib/generators/factory_bot/model/model_generator.rb After that, try to guess how it works: rails g factory_bot:model Car name speed:integer The result is: create test/factories/cars.rb And the content: # Read about factories at https://github.com/thoughtbot/factory_girl FactoryBot.define do factory :car do name “MyString” speed 1 end end Remember, … Read more

Factory Girl – what’s the purpose?

Gems like Factory Girl and Sham allow you to create templates for valid and re-usable objects. They were created in response to fixtures which where fixed records that had to be loaded into the database. They allow more customization when you instantiate the objects and they aim to ensure that you have a valid object … Read more

FactoryGirl and polymorphic associations

Although there is an accepted answer, here is some code using the new syntax which worked for me and might be useful to someone else. spec/factories.rb FactoryGirl.define do factory :musical_user, class: “User” do association :profile, factory: :musician #attributes for user end factory :artist_user, class: “User” do association :profile, factory: :artist #attributes for user end factory … Read more

Find or create record through factory_girl association

You can to use initialize_with with find_or_create method FactoryGirl.define do factory :group do name “name” initialize_with { Group.find_or_create_by_name(name)} end factory :user do association :group end end It can also be used with id FactoryGirl.define do factory :group do id 1 attr_1 “default” attr_2 “default” … attr_n “default” initialize_with { Group.find_or_create_by_id(id)} end factory :user do association … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)