Ich möchte ein Modell mit RSpec testen, aber ich bin wahrscheinlich auf einen Tippfehler gestoßen, den ich einfach nicht finden kann. Kann mir jemand bitte ein bisschen helfen? Ich habe lange damit zu kämpfen und kann einfach keine Fehler finden. Danke im Voraus!
user_spec.rb
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
it "is invalid without an e-mail"
it "is invalid without a correct e-mail"
it "is invalid without a password"
it "is invalid without a matching password confrimation"
end
user.rb
FactoryGirl.define do
factory :user do |f|
f.email "[email protected]"
f.password "Ruby"
f.password_confrimation "Ruby"
end
end
spec_helper.rb
# This file is copied to spec/ when you run 'Rails generate rspec:install'
ENV["Rails_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/Rails'
require 'rspec/autorun'
require 'factory_girl'
# Requires supporting Ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-Rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
error
Factory not registered: user
Sie haben Ihre werkseitige Definition in der falschen Datei, entsprechend Ihrer Frage in user.rb. Dies muss sich in einer factories.rb in Ihrem Testordner (spec) befinden, wenn Sie rspec verwenden
# user.rb
FactoryGirl.define do
factory :user do |f|
f.email "[email protected]"
f.password "Ruby"
f.password_confrimation "Ruby"
end
end
Ändern Sie oben, (Sie brauchen auch nicht die Variable f).
# spec/factories.rb
FactoryGirl.define do
factory :user do
email "[email protected]"
password "Ruby"
password_confrimation "Ruby"
end
end
Stellen Sie außerdem sicher, dass sich gem 'factory_girl_Rails'
in Ihrer Gemfile
befindet, anstatt nur gem 'factory_girl'
.
Ich hatte folgendes Problem mit der Struktur in meinem Ordner factories /
- factories/
-- artists.rb
-- techniques.rb
artists.rb
FactoryBot.define do
factory :artist do
name 'Michael'
technique FactoryBot.create :technique
end
end
techniques.rb
FactoryBot.define do
factory :technique do
name 'Some name'
end
end
Es war also ein Künstler zum Laden, bevor das Technikobjekt geladen wurde. Es konnte es also nicht finden. Die Lösung besteht darin, verschachtelte FactoryBot-Funktionen nicht in Fabriken zu verwenden oder Ihre verschachtelten Fabriken in etwas umzubenennen, das vor Ihrer übergeordneten Fabrik steht.
Ich habe gerade meine Technikfabrik umgezogen. zu factories.rb
und dort definiert. Und das Problem wurde gelöst.
Ich hatte das gleiche Problem und das einzige, was ich hinzugefügt habe, war eine factories.rb-Datei. Alle Tests bestanden daraus.