-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FactoryBot doesn't see Doorkeeper classes #1043
Comments
Doorkeeper 4.3 uses FactoryBot, and Doorkeeper 4.2.6 uses FactoryGirl. What gem do you have? Also, could you please provide your |
i'm now use 4.2.6 with FactoryBot.
|
And what about yhe factories? Do you have RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end ? |
i got
in rails_helper.rb I don't have that record:
will this solve problem? |
It loads factories for the gem and suggested by the official docs. I will try to reproduce the error a little bit later |
must say, that doesnt work for me |
It is a brandly new app or some legacy project? Did you do I created a new project with next Gemfile: source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.3.1'
gem 'rails', '~> 5.2.0.beta2'
gem 'sqlite3'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'duktape'
gem 'doorkeeper', '~> 4.3'
group :development, :test do
gem 'factory_bot_rails'
gem 'rspec-rails', '~> 3.7'
end
group :development do
gem 'web-console', '>= 3.3.0'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# spec/factories/access_token.rb
FactoryBot.define do
factory :access_token, class: Doorkeeper::AccessToken do
end
end And spec: # spec/models/token_spec.rb
require 'rails_helper'
RSpec.describe 'token' do
it 'test' do
expect(FactoryBot.build(:access_token)).to be_valid
end
end Everything is OK. |
educational app
yes, i did. i have config/initializers/doorkeeper.rb
lucky you) |
Yep, it would be great if you have this one public! |
@slim1979 yes, I see the problem, but can't find the source of it. As I can see from the trace debugging, ActiveSupport lazy hooks are not invoked, so models didn't loaded. Can't find the reason why it is happens, maybe some initializer / configuration / gem side effects.. If you will add |
Analyzed trace and it looks like in your case AS doesn't load dependencies or do it in other way https://gist.github.com/nbulaj/5ce408c58a6fde239b8e71653ce5d568 I was using Rails 5.2, and it uses ActiveStorage that depends on ActiveRecord. So it loads AR somehow and my tests were green. If I remove |
my mentor thinking, that this is about lazy_load helper, which will load all only if AS initialized, but factories loads earlier and this cause a problem |
He is right about the helpers. Doorkeeper models must be loaded at the time when ActiveRecord are loaded using ActiveSupport lazy hooks (in this case we can configure some AR options before Doorkeeper models would be loaded). I'l try to investigate the problem and solve it for 4.3.1 release. |
Hi @slim1979 . The problem is in To solve this problem you can replace Related issue exists on the P.S. I recommend you to move require statements in your |
will try it later.... thanks a lot for your investigation) |
@slim1979 any news here? |
@nbulaj, hi there. no, nothing new... i had an conversation with my mentor about this situation and he told me, what this will never been solved by any sides, because noone want to change its product, as saying. this is noone bug, just incompatibility. i willn't change my doorkeeper version right now, because it doesn't make sense for me. maybe, someone will see problem solvation in this issue... i will be glad |
So because of it is not a bug in Doorkeeper itself I will close this issue now. The only solution I see (for Doorkeeper) is to manually trigger AR::Base (before factory bot rails initializer), but you are right - I don't think I would do this for some testing gem. As |
I tried this, will work for you until this issue is solved on require 'factory_bot'
require 'factory_bot_rails/generator'
require 'rails'
module FactoryBot
class Railtie < Rails::Railtie
initializer "factory_bot.set_fixture_replacement" do
FactoryBotRails::Generator.new(config).run
end
initializer "factory_bot.set_factory_paths" do
FactoryBot.definition_file_paths = [
Rails.root.join('factories'),
Rails.root.join('test', 'factories'),
Rails.root.join('spec', 'factories')
]
end
config.after_initialize do
# This line is the one that fixes it:
ActiveSupport.on_load(:active_record) { FactoryBot.find_definitions }
if defined?(Spring)
Spring.after_fork { FactoryBot.reload }
end
end
end
end I'll post this as a PR on Factory Bot Rails repo |
As described in doorkeeper-gem#1043, factories defined by doorkeeper are unable to be loaded by host apps using the `factory_bot_rails` gem (receiving errors such as `uninitialized constant Doorkeeper::AccessGrant`) Documentation for factory_bot was later added to explain an easy fix for this: instead of defining the factory's `class: ` with a named constant, wrap the constant in a string. By doing so, the constant isn't evaluated until the factory actually runs, at which point the `Doorkeeper::` models have all been loaded. Fixes thoughtbot/factory_bot#1188
As described in doorkeeper-gem#1043, factories defined by doorkeeper are unable to be loaded by host apps using the `factory_bot_rails` gem (receiving errors such as `uninitialized constant Doorkeeper::AccessGrant`) Documentation for factory_bot was later added to explain an easy fix for this: instead of defining the factory's `class: ` with a named constant, wrap the constant in a string. By doing so, the constant isn't evaluated until the factory actually runs, at which point the `Doorkeeper::` models have all been loaded. Fixes thoughtbot/factory_bot#1188
I was able to get around this issue by putting quotes around the class name when defining the factory: factory :access_token, class: 'Doorkeeper::AccessToken' do
...
end High five to @sman591 for this excellent commit message:
|
good day for everyone
when using doorkeeper-4.3.0, a have a problem with FactoryBot. He refused to see doorkeeper classes:
and other
rollback to 4.2.6 solve this problem
The text was updated successfully, but these errors were encountered: