1
votes

I'm new to Ruby on rails and programming in general. In an assignment I'm doing, I was asked to create a test where a User visits his on profile. Rspec, Devise and capybara gems are installed.

Here is my profiles_spec:

require 'rails_helper'

describe "Visiting profiles" do 

  include TestFactories
  before do 
    @user = authenticated_user
    @post = associated_post(user: @user)
    @comment = Comment.new(user: @user, post: @post, body:"A comment")
    allow(@comment).to receive(:send_favorite_emails)
    @comment.save
    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)

  end

  describe "not signed in" do

    it "shows profile" do 
      visit user_path(@user)
      expect(current_path).to eq(user_path(@user))

      expect(page).to have_content(@user.name)
      expect(page).to have_content(@post.title)
      expect(page).to have_content(@comment.body)
    end

  end

  describe "user visting own profile" do 

  it "shows profile" do 
    visit user_path(current_user)

    expect(current_path).to eq(user_path(user))
    expect(page).to have_content(user.name)
    expect(page).to have_content(@post.title)
    expect(page).to have_content(@comment.body)
    end
  end
end

Here is my TestFactories:

module TestFactories 
  include Warden::Test::Helpers
  Warden.test_mode!
    def associated_post(options = {})
   post_options = {
     title: 'Post title',
     body: 'Post bodies must be pretty long.',
     topic: Topic.create(name: 'Topic name',description: 'the description of a topic must be long'),
     user: authenticated_user
   }.merge(options)

   Post.create(post_options)
 end

 def authenticated_user(options = {})
  user_options = { email: "email#{rand}@fake.com", password: 'password'}.merge(options)
  user = User.new( user_options)
  user.skip_confirmation!
  user.save
  user 
 end

 FactoryGirl.define do
  factory :user do
    email '[email protected]'
    password 'f4k3p455w0rd'
    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)
    # if needed
    # is_active true
  end
end

end 

Here is my User model:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :votes, dependent: :destroy
  has_many :favorites, dependent: :destroy

  mount_uploader :avatar, AvatarUploader

  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end

  def favorited(post)
    favorites.where(post: post.id).first
  end

  def voted(post)
    votes.where(post: post.id).first
  end

end

When I run the profiles test, I get this error:

`<module:TestFactories>': uninitialized constant TestFactories::FactoryGirl (NameError)

I'm not sure if i'm using warden the right way.

Thank you.

2

2 Answers

0
votes

The error is because you have not included FactoryGirl In your TestFactories-module.

Your factory looks pretty messy.

In my projects I have a folder structure with spec/factories where I put factories.

For example I would name this users.rb:

FactoryGirl.define do
  factory :user do
    email
    password '12345678'
  end
end

To skip confirmation you can add:

confirmed_at: { Time.zone.now}

In a separate factory called shared I put:

  sequence(:email) { |n| "name#{n}@domain.se" }

Then when I want a user in a test I use

let(:user) { FactoryGirl.create(:user

And you should be able to use

login_as

I'm not sure how that work because I mostly use CanCanCommunity.

Sorry for messy post, written on phone.

0
votes

The uninitialized constant...FactoryGirl error message says that FactoryGirl isn't available.

The options are to install and configure the factory girl gem, or to avoid it until its needed.

For this spec, it looks like factory girl isn't adding anything, so consider removing these references to it (and avoid installing the factory girl gem for now):

  • The line user = FactoryGirl.create(:user) from profiles_spec.rb
  • The entire FactoryGirl.define do...end block in TestFactories

It appears that the existing @user object is adequate for what the spec needs. user and current_user don't seem to do anything that @user couldn't be used for. Apologies if I've missed something.