【发布时间】:2014-08-27 13:55:37
【问题描述】:
好的,我正在尝试使用 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
integration_spec.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)>'
【问题讨论】:
-
那么您对用户和组织都有“authenticates_with_sorcery”?
-
嗯...哇。是的!这是我的问题。这就是我匆忙复制 user.rb 文件以创建我的组织模型的结果。随意张贴作为答案,我会给你信用。
-
酷,很高兴它已排序。 :)
标签: ruby-on-rails ruby-on-rails-4 rspec model-associations fabrication-gem