Ruby on rails 测试用户访问自己的个人资料与rspec水豚,典狱长宝石轨道

Ruby on rails 测试用户访问自己的个人资料与rspec水豚,典狱长宝石轨道,ruby-on-rails,ruby,rspec,capybara,warden,Ruby On Rails,Ruby,Rspec,Capybara,Warden,我对RubyonRails和一般编程都是新手。 在我正在做的一项任务中,我被要求创建一个测试,用户在其中访问他的个人资料。 安装了Rspec、Desive和capybara gems 这是我的个人资料规格: require 'rails_helper' describe "Visiting profiles" do include TestFactories before do @user = authenticated_user @post = associat

我对RubyonRails和一般编程都是新手。 在我正在做的一项任务中,我被要求创建一个测试,用户在其中访问他的个人资料。 安装了Rspec、Desive和capybara gems

这是我的个人资料规格:

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
以下是我的测试工厂:

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 'test@example.com'
    password 'f4k3p455w0rd'
    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)
    # if needed
    # is_active true
  end
end

end 
以下是我的用户模型:

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
class用户
当我运行配置文件测试时,出现以下错误:

`<module:TestFactories>': uninitialized constant TestFactories::FactoryGirl (NameError)
“:未初始化的常量TestFactorys::FactoryGirl(NameError)
我不确定我是否用对了典狱长


谢谢。

错误是因为您的
测试工厂模块中没有包含FactoryGirl

你的工厂看起来很乱

在我的项目中,我有一个带有
spec/factories
的文件夹结构,我把工厂放在那里

例如,我将此命名为
users.rb

FactoryGirl.define do
  factory :user do
    email
    password '12345678'
  end
end
要跳过确认,您可以添加:

confirmed_at: { Time.zone.now}
在一个名为
shared
我说:

然后,当我在测试中需要一个用户时,我使用

let(:user) { FactoryGirl.create(:user
你应该能够使用

login_as
我不确定这是怎么回事,因为我主要使用CanCanCommunity


很抱歉在电话上写了一篇凌乱的帖子。

未初始化常量…FactoryGirl
错误消息说,
FactoryGirl
不可用

选项是安装和配置factory girl gem,或者在需要时避免使用它

对于这个规范,看起来工厂女工没有添加任何东西,所以考虑删除这些引用(现在不要安装工厂女孩GEM):

  • user=FactoryGirl.create(:user)
    from
    profiles\u spec.rb
  • 整个
    FactoryGirl.define do…end
    块位于
    TestFactories

现有的
@user
对象似乎足以满足规范的需要
user
当前用户
似乎没有做任何
@user
无法使用的事情。如果我错过了什么,我会道歉。

嘿,艾略特,谢谢你的回复。我曾尝试在@user中使用designe helpers:sign_,但我收到一个错误:``失败/错误:无法从回溯NoMethodError中找到匹配的行:未定义nil:NilClass的方法`env```根据分配说明,它声明designe helpers不能与spec/feautures一起工作
login_as