Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 FactoryGirl在数据库中找不到实例_Ruby On Rails_Ruby_Testing_Factory Bot_Minitest - Fatal编程技术网

Ruby on rails FactoryGirl在数据库中找不到实例

Ruby on rails FactoryGirl在数据库中找不到实例,ruby-on-rails,ruby,testing,factory-bot,minitest,Ruby On Rails,Ruby,Testing,Factory Bot,Minitest,在尝试使用FactoryGirl初始化数据时,我遇到了一个问题,无法访问以前创建的数据 假设我有3种不同的型号:Product、CartItem和OrderItem。这些是基本规则: CartItem和OrderItem属于产品,是必需的 产品具有唯一标识符“名称” 我的工厂文件设置如下: 产品 FactoryGirl.define do factory :product do name "A Product" end end CartItem FactoryGirl.de

在尝试使用FactoryGirl初始化数据时,我遇到了一个问题,无法访问以前创建的数据

假设我有3种不同的型号:Product、CartItem和OrderItem。这些是基本规则:

  • CartItem和OrderItem属于产品,是必需的
  • 产品具有唯一标识符“名称”
我的工厂文件设置如下:

产品

FactoryGirl.define do
  factory :product do
    name "A Product"
  end
end
CartItem

FactoryGirl.define do
  factory :cart_item do
    association :product do
        Product.find_by(name: "A Product") || FactoryGirl.create(:product)
    end
  end
end
FactoryGirl.define do
  factory :order_item do
    association :product do
        Product.find_by(name: "A Product") || FactoryGirl.create(:product)
    end
  end
end
OrderItem

FactoryGirl.define do
  factory :cart_item do
    association :product do
        Product.find_by(name: "A Product") || FactoryGirl.create(:product)
    end
  end
end
FactoryGirl.define do
  factory :order_item do
    association :product do
        Product.find_by(name: "A Product") || FactoryGirl.create(:product)
    end
  end
end
现在,在一个测试中,我首先使用这个调用
FactoryGirl.create(:cart\u item)

一切正常。因为没有产品,它会创建一个新产品,然后将其分配给CartItem

接下来,我尝试使用这个调用创建OrderItem
FactoryGirl.create(:order\u item)

这一次,当我运行它时,它失败了,错误是
验证失败,名称已被取下

当尝试创建名为“a Product”的新产品时,此操作失败,该产品是通过调用create CartItem创建的

但是,这甚至不应该尝试创建新的产品实例,因为我使用此
产品设置OrderItem的产品。find_by(“产品”)| | FactoryGirl.create(:Product)
,它应该在创建新的产品实例之前首先尝试查找该产品实例


关于为什么会发生这种情况,您有什么想法吗?

这只是一个想法,但请尝试在FactoryGirl配置中注释FactoryGirl.lint

更新

我相信您的问题在于您使用
关联的方式。我看不到任何地方,这种联系会像你定义它的方式那样受到阻碍

你想做的是这样的事情

factory :cart_item do
  product { Product.find_by(name: "A Product") || association(:product) }       
end
这似乎是错误的,因为你在创造非决定论。相反,您应该创建一个记录,并在测试中直接将其分配给工厂

FactoryGirl.define do
  factory :cart_item do
    association :product 
  end
end

FactoryGirl.define do
  factory :order_item do
    association :product
  end
end
然后在你的测试中

product = FactoryGirl.create(:product)
cart_item = FactoryGirl.create(:cart_item, product: product)
order_item = FactoryGirl.create(:order_item, product: product)

我也试过做这个
除非是Product.find_by(name:“A Product”)do FactoryGirl.create(:Product)end
,但同样的问题发生了我的错,那是一个打字错误。我的代码实际上是在做
Product.find_by(name:“A Product”)
。但是对于“name”声明,它仍然存在相同的问题实际上,您在FactoryGirl上的
关联的语法是从哪里找到的。我不认为
关联
需要一个块…或者至少它不是文档。我只是根据Ruby的一般工作原理诚实地尝试了它,并且它工作了。我需要一种方法将模型与使用Desive的用户关联起来,但您不能通过简单地执行
FactoryGirl.create(:User)
来创建使用Desive的用户。相反,您必须转到(:user)
@user\u attr=FactoryGirl.attributes\u,后跟“user.create(@user\u attr)”。因为关联调用不是来自工厂,所以我用
do
语句替换它,并将这两个调用放在其中。它是有效的。因为它和用户一起工作,我想它也会对其他人起作用。我没有关注你之前的评论,因为他们没有真正关注你在做什么?