gpt4 book ai didi

ruby-on-rails - factory_girl 未定义方法 `save!' 为

转载 作者:行者123 更新时间:2023-11-29 13:55:53 25 4
gpt4 key购买 nike

## app/models/armor_type.rb
class ArmorType < ActiveRecord::Base
validates :name, presence: true
...
end

## spec/models/armor_type_spec.rb
require 'rails_helper'
RSpec.describe ArmorType, type: :model do
it "has a valid name" do
armor_type = create(:armor_type)
end
end

## spec/factories/armor_types.rb
FactoryGirl.define do
factory :armor_type do
name "cloth"
end
end

## spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

## spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'support/factory_girl'
require 'capybara/rspec'

## Gemfile
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
end

我似乎无法让 factory_girl 正常工作。我在 testApp 使用 SQLite3 测试数据库创建并且它工作,但是使用我当前使用 PostgreSQL 测试数据库的应用程序我收到以下错误;

          1) ArmorType has a valid name
Failure/Error: armor_type = create(:armor_type)
NoMethodError:
undefined method `save!' for #<ArmorType:0x000000071a8b58 @name="cloth">
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# ./.bundle/gems/activesupport-4.2.3/lib/active_support/notifications.rb:166:in `instrument'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/models/armor_type_spec.rb:6:in `block (2 levels) in <top (required)>'
# .bundle/binstubs/rspec:16:in `load'
# .bundle/binstubs/rspec:16:in `<main>'

我继续使用 PostgreSQL 测试数据库创建了另一个测试应用程序,但我遇到了同样的错误。任何帮助将不胜感激。

干杯!

最佳答案

我在测试的早期阶段无意中创建了一个文件“armor_type.rb”,导致了这个错误。它与 factory_girl 和 rspec 无关。

新手的错误

关于ruby-on-rails - factory_girl 未定义方法 `save!' 为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32106061/

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