gpt4 book ai didi

ruby - 设置 FactoryGirl 并在 Grape 和 RSpec 中使用它?

转载 作者:行者123 更新时间:2023-11-28 21:10:27 26 4
gpt4 key购买 nike

在我的 Grape ruby​​ 项目中。我将我的模型分离到一个 gem 中,这样我就可以在我的 ruby​​ 项目之间使用它。

现在的问题是我的 activerecords,假设我正在处理 User 模型,现在它看起来像这样:

module MyApp
module Core
class User < ActiveRecord::Base
self.table_name = 'users'
end
end
end

我正在使用像这样的工厂女孩​​:

FactoryGirl.define do
factory :user do

first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password { "12345678" }
password_confirmation { "12345678" }
end
end

假设我有以下 rspec 测试:

require 'spec_helper'

describe MyApp::Core::User do
it "has name assigned" do
user = build(:member, first_name: 'Eki', last_name: 'Eqbal')
expect(user.first_name).to eq('Eki')
expect(user.last_name).to eq('Eqbal')
end
end

当我尝试运行测试时:

⇒  bundle exec rspec spec/unit/users_spec.rb
warning: parser/current is loading parser/ruby22, which recognizes
warning: 2.2.3-compliant syntax, but you are running 2.2.0.
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
F

Failures:

1) MyApp::Core::User has name assigned
Failure/Error: user = build(:user, first_name: 'Eki', last_name: 'Eqbal')
NameError:
uninitialized constant User
# ./spec/unit/users_spec.rb:37:in `block (2 levels) in <top (required)>'

Finished in 0.14788 seconds
1 example, 1 failure

Failed examples:

最佳答案

代替:

factory :user do

尝试:

factory :user, class: MyApp::Core::User do

FactoryGirl 根据工厂名称猜测类名,所以如果它在这样的模块中,它不会找到它。

关于ruby - 设置 FactoryGirl 并在 Grape 和 RSpec 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31544835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com