Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 工厂女孩未定义方法`保存';对于 ##app/models/armor_type.rb 类ArmorType_Ruby On Rails_Postgresql_Factory Bot - Fatal编程技术网

Ruby on rails 工厂女孩未定义方法`保存';对于 ##app/models/armor_type.rb 类ArmorType

Ruby on rails 工厂女孩未定义方法`保存';对于 ##app/models/armor_type.rb 类ArmorType,ruby-on-rails,postgresql,factory-bot,Ruby On Rails,Postgresql,Factory Bot,我似乎无法让工厂女孩正常工作。我使用SQLite3测试数据库在testApp中创建了它,它工作正常,但是使用我当前使用PostgreSQL测试数据库的应用程序,我得到以下错误: ## app/models/armor_type.rb class ArmorType < ActiveRecord::Base validates :name, presence: true ... end ## spec/models/armor_type_spec.rb require 'rails_h

我似乎无法让工厂女孩正常工作。我使用SQLite3测试数据库在testApp中创建了它,它工作正常,但是使用我当前使用PostgreSQL测试数据库的应用程序,我得到以下错误:

## 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
1)ArmorType具有有效名称
失败/错误:装甲类型=创建(:装甲类型)
命名错误:
未定义的方法“save!”为了#
#./.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:在“结果块”中
#./.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_registrator.rb:20:在`定义中的块_singular_strategy_method'
#./spec/models/armor\u type\u spec.rb:6:in'block(2层)in'
#.bundle/binstubs/rspec:16:in“load”
#.bundle/binstubs/rspec:16:in`'
我已经使用PostgreSQL测试数据库创建了另一个测试应用程序,但我收到了相同的错误。任何帮助都将不胜感激


干杯

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


NewbieErrors在测试的早期阶段,我无意中创建了一个文件'armor_type.rb',导致了这个错误。这与工厂女孩无关,也与rspec无关


新手错误这不会修复任何问题,但在验证时,您是否应该在非持久记录上执行这些操作?关于
保存armor\u types.rb
和一个
FactoryGirl.define do…
?另外,我不确定rails\u助手中的
是否需要“支持/工厂女孩”
。好吧,应该是指宝石厂的女孩。第三件要检查的事情是:您是否为rails gem安装了FactoryGirl?其中有两个,一个是专门针对rails的。我编辑了我的问题,将工厂(愚蠢的misake,抱歉)和Gemfile包括在内。@Mauddev“支持/工厂女孩”是根据您使用它的方式,我在链接中找不到它。也许再看看我的评论(关于
rails\u helper
。另外,在您发布的工厂定义中,缺少一个
end
。这不会修复任何问题,但在验证时,您可能应该在非持久记录上执行这些操作?关于
保存!
,这真的很奇怪。您可以粘贴您的装甲类型工厂以获取更多信息吗你真的建立了一个工厂吗?比如:你是否有一个dir spec/factories文件
armor\u types.rb
和一个
FactoryGirl.define do…
?另外,我不确定rails\u助手中的
需要'support/factory\u girl'
。好的,它应该指向gem factory\u girl。还有第三件要检查的事情:你是否安装了t他是rails gem的FactoryGirl吗?有两个,其中一个是rails的。我编辑了我的问题,将工厂(愚蠢的错误,对不起)和Gemfile包括在内。@Mauddev“支持/工厂女孩”是根据您使用它的方式,我在链接中找不到它。也许可以再次阅读我的评论(关于
rails\u helper
。此外,在这里发布的工厂定义中,缺少一个
end
          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>'