ruby-on-railsrspeccapybarafactory-bot

FactoryGirl Rspec Rails user has a different name


I'm a hobby developer, no education. I two questions relating to a 2017 rails project I want to write 50 feature specs for (feature specs because it's rails 5.0.1.rc2, system specs came in 5.1).

in this project the User is called a Fan.

gem 'rails', '>= 5.0.1.rc2', '< 5.1'
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
  gem 'rspec-rails', '~> 3.5.0'
  gem 'puma', '~> 3.0'
end

group :test do
  gem 'capybara', '~> 2.10.1'
  gem 'factory_girl_rails', '~> 4.7'
  gem 'selenium-webdriver', '~> 3.0.0'
  gem "database_cleaner", "~> 1.5"
  gem 'shoulda-matchers', '~> 3.1'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

Here's my first rspec (written in 2017).

require "rails_helper"

RSpec.feature "Fans can sign in" do
  let!(:fan) { FactoryGirl.create(:fan) }

  scenario "with valid credentials" do
    visit "/"
    click_link "Log in"
    fill_in "Email", with: fan.email
    fill_in "Password", with: "password"
    click_button "Sign in"
    expect(page).to have_content "NEWS"
    # expect(page).to have_content "Signed in successfully."
    # expect(page).to have_content "#{fan.email}"
  end
end
FactoryGirl.define do
  factory :fan do
    # sequence(:email) { |n| "test#{n}@example.com" }
    email "email"
    password "password"
    first_name "firstname"
    last_name "lastname"
    postal_code "P7A9J5"
    city "Amsterdam"
    state "Ontario"
    country "Canada"
    confirmed_at Time.now
    ...
  end
end

Question 1:

When I run the rspec, if I binding.pry the "/" page controller all I can find is :fan

But on the site the current user is called current_fan

app/channels/application_cable/connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_fan
etc

This will result in the entire site not working with FactoryGirl. Is there some way to associate the two?

Question 2:

On my local server, I have an instance variable that has to be present (@required = RequiredClass.find(1)), that gets called in the / controller. However, it's obvious I haven't attached rspec to my local server as the required instance doesn't exist, and rspec goes in an infinite loop for 5 times then cancels.

How do I indicate my local database and set it up?

I tried adding

routes.default_url_options[:host] = 'localhost:3000'

to the top of config/environments/development.rb, but it made bundle exec rails routes stop working. do I have to have rails s running?

Question 3

How would I hook up the running live version of the site to perform these tests on the actual website?

Much appreciated in advance


Solution

  • FactoryBot (nee Girl) doesn't interact directly with your application during feature specs. It's used to create records in the database which are then read by your application running in a separate thread or process. Additionally the fact that current_fan is defined in your action cable class doesn't mean it's defined in your controllers. Setup of all of this should be done via configuring the test environment, however you're running a setup that is so far out of date you'll likely have issues finding accurate details about that. Update if you can.

    Oh, and you don't run against the live version of the site, that would be a huge risk of wiping out all the data when you're involving database_cleaner in there.