1

好的,我正在尝试将 Fabricator 与我的 Rspec 测试一起使用来模拟一些测试数据。但是,我在belongs_to协会方面遇到了一些麻烦。这是我到目前为止所拥有的:

用户.rb

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  belongs_to :organization

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates_presence_of :full_name
  validates_presence_of :email
  validates_uniqueness_of :email, on: :create
  validates_format_of :email, with: VALID_EMAIL_REGEX, on: :create
  validates_presence_of :password, on: :create
  validates_confirmation_of :password
end

组织.rb

class Organization < ActiveRecord::Base
  authenticates_with_sorcery!

  has_many :users, dependent: :destroy

  accepts_nested_attributes_for :users, :allow_destroy => true

  validates_presence_of :name
end

集成规范.rb

require 'rails_helper'

describe "Shopping Cart Requests" do
  let!(:user) { Fabricate(:user) }

  before(:each) do
    login_user_post("admin@example.com", "password")
  end

  context "when I visit the shopping cart" do
    it " show the logged in users' cart items " do
      #Test stuff
    end
  end
end

user_fabricator.rb

Fabricator(:user) do
  organization { Fabricate(:organization) }
  email { "admin@example.com" }
  password { "password" }
  full_name { Faker::Name.name }
  is_admin { true }
  salt { "asdfghjkl123456789" }
  crypted_password { Sorcery::CryptoProviders::BCrypt.encrypt("secret", "asdasdastr4325234324sdfds") }
  activation_state { 'active' }
end

organization_fabricator.rb

Fabricator(:organization) do
  name { Faker::Company.name }
  website { Faker::Internet.url }
  description { Faker::Lorem.paragraph }
  access_code { Faker::Internet.password(10, 20) }
end

这是我在运行测试时遇到的错误:

Failure/Error: let!(:user) { Fabricate(:user) }
     NoMethodError:
       undefined method `crypted_password' for #<Organization:0x007f80ee0a44e0>
     # ./spec/features/integration_spec.rb:4:in `block (2 levels) in <top (required)>'
4

1 回答 1

3

authenticates_with_sorcery!在您的组织应用程序中。

如果您不打算对组织进行身份验证,则应删除该行。

干杯

于 2014-08-27T15:06:50.580 回答