1

好吧,我想用我的Rspec測試的加工器來模擬一些測試數據。但是,我在belongs_to關聯中遇到了一些問題。這是我到目前爲止有:Rails 4 - 在Rspec測試中通過加工器創建關聯的錯誤

user.rb

class User < ActiveRecord::Base 
    authenticates_with_sorcery! 

    belongs_to :organization 

    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][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 

organization.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("[email protected]", "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 { "[email protected]" } 
    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)>' 
+0

因此,您對用戶和組織都有「authenticates_with_sorcery」嗎? – SteveTurczyn 2014-08-27 14:49:01

+0

嗯......哇。對!這是我的問題。這就是我得到匆忙,只是複製user.rb文件來創建我的組織模型。隨意張貼作爲答案,我會給你信用。 – ryanpitts1 2014-08-27 14:54:42

+0

很酷,很高興它被分類。 :) – SteveTurczyn 2014-08-27 15:07:06

回答

3

你有authenticates_with_sorcery!

如果您不打算驗證組織,則應刪除該行。

乾杯

相關問題