我之前听过很多次这个术语(谈论编程时),但是找不到任何解释是什么意思.有什么好的文章或解释吗?我没有找到任何值得一提的东西.
由于内容类型冲突,我无法将Django fixtures加载到我的MySQL数据库中.首先,我尝试从我的应用程序转储数据,如下所示:
./manage.py dumpdata escola > fixture.json
Run Code Online (Sandbox Code Playgroud)
但我不断错过外键问题,因为我的应用程序"escola"使用其他应用程序中的表.我一直在添加额外的应用程序,直到我这样做:
./manage.py dumpdata contenttypes auth escola > fixture.json
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试将数据作为测试夹具加载时,问题是以下约束违规:
IntegrityError: (1062, "Duplicate entry 'escola-t23aluno' for key 2")
Run Code Online (Sandbox Code Playgroud)
似乎问题是Django正在尝试动态地重新创建具有与夹具中的主键值冲突的不同主键值的内容类型.这似乎与此处记录的错误相同:http://code.djangoproject.com/ticket/7052
问题是推荐的解决方法是转储我正在做的contenttypes应用程序!?是什么赋予了?如果它有所不同,我确实有一些自定义模型权限,如下所示:http://docs.djangoproject.com/en/dev/ref/models/options/#permissions
我正在使用py.test来测试包含在python类MyTester中的一些DLL代码.为了验证目的,我需要在测试期间记录一些测试数据,然后再进行更多处理.由于我有很多测试_...文件,我想在大多数测试中重用测试器对象创建(MyTester实例).
由于测试对象是获得对DLL的变量和函数的引用的对象,我需要将DLL的变量列表传递给每个测试文件的测试对象(要记录的变量对于test_ .. .文件).列表的内容应用于记录指定的数据.
我的想法是以某种方式这样做:
import pytest
class MyTester():
def __init__(self, arg = ["var0", "var1"]):
self.arg = arg
# self.use_arg_to_init_logging_part()
def dothis(self):
print "this"
def dothat(self):
print "that"
# located in conftest.py (because other test will reuse it)
@pytest.fixture()
def tester(request):
""" create tester object """
# how to use the list below for arg?
_tester = MyTester()
return _tester
# located in test_...py
# @pytest.mark.usefixtures("tester")
class TestIt():
# def __init__(self):
# self.args_for_tester = ["var1", "var2"]
# # how to …
Run Code Online (Sandbox Code Playgroud) 关于Django灯具的一个问题是你必须指定每个型号的主键.有没有办法创建灯具而无需为每一行指定主键?
在我的应用程序中,我想在新用户注册时在某些表中创建条目.例如,我想创建一个userprofile,然后引用他们的公司和其他一些记录.我用post_save信号实现了这个:
def callback_create_profile(sender, **kwargs):
# check if we are creating a new User
if kwargs.get('created', True):
user = kwargs.get('instance')
company = Company.objects.create(name="My Company")
employee = Employee.objects.create(company=company, name_first=user.first_name, name_last=user.last_name)
profile = UserProfile.objects.create(user=user, employee=employee, partner=partner)
# Register the callback
post_save.connect(callback_create_profile, sender=User, dispatch_uid="core.models")
Run Code Online (Sandbox Code Playgroud)
运行时效果很好.我可以使用admin创建一个新用户,其他三个表也可以获得合理的条目.(除此之外,员工因为user.first_name和user.last_name在保存时未在管理员表单中填写.我仍然不明白为什么这样做)
当我运行我的测试套件时出现问题.在此之前,我创建了一堆灯具来在表格中创建这些条目.现在我收到一条错误,指出:
IntegrityError: duplicate key value violates unique constraint "core_userprofile_user_id_key"
Run Code Online (Sandbox Code Playgroud)
我想这是因为我已经在id为"1"的灯具中创建了公司,员工和个人资料记录,现在post_save信号正在尝试重新创建它.
我的问题是:我可以在运行灯具时禁用此post_save信号吗?我可以检测到我作为测试套件的一部分运行而不是创建这些记录吗?我现在应该从灯具中删除这些记录吗(虽然信号只设置默认值而不是我想要测试的值)?为什么夹具加载代码不会覆盖创建的记录?
人们如何做到这一点?
我正在使用Rails 2.2项目来更新它.我正在用工厂替换现有的灯具(使用factory_girl)并且遇到了一些问题.问题在于表示具有查找数据的表的模型.当我创建一个包含两个具有相同产品类型的产品的购物车时,每个创建的产品都会重新创建相同的产品类型.这是ProductType模型上唯一验证的错误.
这是一个单元测试,我创建一个Cart并将它们组合在一起.我不得不这样做以解决问题.但这仍然证明了这个问题.我会解释一下.
cart = Factory(:cart)
cart.cart_items = [Factory(:cart_item,
:cart => cart,
:product => Factory(:added_users_product)),
Factory(:cart_item,
:cart => cart,
:product => Factory(:added_profiles_product))]
Run Code Online (Sandbox Code Playgroud)
添加的两个产品属于同一类型,每个产品创建时都会重新创建产品类型并创建重复项.
生成的错误是:"ActiveRecord :: RecordInvalid:验证失败:名称已被占用,代码已被占用"
此示例的解决方法是覆盖正在使用的产品类型并传入特定实例,因此只使用一个实例."add_product_type"提前获取并传入每个购物车项目.
cart = Factory(:cart)
prod_type = Factory(:add_product_type) #New
cart.cart_items = [Factory(:cart_item,
:cart => cart,
:product => Factory(:added_users_product,
:product_type => prod_type)), #New
Factory(:cart_item,
:cart => cart,
:product => Factory(:added_profiles_product,
:product_type => prod_type))] #New
Run Code Online (Sandbox Code Playgroud)
使用factory_girl与"pick-list"类型的关联的最佳方法是什么?
我喜欢为厂家定义为包含,而不必进行组装测试的一切,虽然我可以住在一起.
工厂/ product.rb
# Declare ProductTypes
Factory.define :product_type do |t|
t.name "None"
t.code "none"
end …
Run Code Online (Sandbox Code Playgroud) 我正在尝试开始为django编写单元测试,我对夹具有一些疑问:
我制作了我的整个项目数据库(不是某些应用程序)的夹具,我想为每个测试加载它,因为看起来只加载某个应用程序的夹具是不够的.
我想将夹具存储在/proj_folder/fixtures/proj_fixture.json中.
我已经FIXTURE_DIRS = ('/fixtures/',)
在我的settings.py中设置了.然后在我的测试用例中我正在尝试
fixtures = ['proj_fixture.json']
Run Code Online (Sandbox Code Playgroud)
但是我的装置没有加载.怎么解决这个问题?如何添加搜索灯具的地方?一般情况下,为每个应用程序中的每个测试加载整个test_db的夹具是否可以(如果它非常小)?谢谢!
我想知道是否有一种方法可以在rails控制台中加载和/或使用fixture.实际上,我想从我的夹具中创建一个用户users.yml
进行一些测试,而不必经历User.new(:name = "John", :email = "..")
每次做的所有"痛苦" .
我目前在测试环境(rails c RAILS_ENV=test
).
如果这不是一个好办法,请说出来.我是Rails的新手所以我在这里学习:)
假设您有以下mongoid文档:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :last_initial
embedded_in :user
end
Run Code Online (Sandbox Code Playgroud)
你如何创建一个工厂女工厂,初始化嵌入的名字和最后的名字?你也将如何处理这种embeds_many
关系呢?
fixtures ×10
django ×5
factory-bot ×2
python ×2
unit-testing ×2
embed ×1
irb ×1
mongoid ×1
mysql ×1
pytest ×1
ruby ×1
signals ×1
test-fixture ×1
ui-testing ×1
uitest ×1