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.