我已经在文件中编写了测试类,并且尝试使用pytest 固定装置,这样我就不必在每个测试函数中创建相同的输入数据。下面是最小的工作示例。
import unittest
import pytest
@pytest.fixture
def base_value():
return 5
class Test(unittest.TestCase):
def test_add_two(self, base_value):
result = base_value + 2
self.assertEqual(result, 7, "Result doesn't match")
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 pytest-3 对此进行测试时,出现以下错误:
类型错误:test_add_two() 缺少 1 个必需的位置参数:'base_value'
这让我感到困惑,因为 base_value 明确作为 的参数之一给出test_add_two。非常感谢任何帮助。
我有一个Django应用程序.我有包含测试数据的.json fixture文件,单元测试使用数据来确认应用程序是否正常工作.我还使用South来迁移我的数据库.
做了几个数据库迁移后,我的灯具已经过时了,因为数据库已迁移,添加一个新的数据库列,例如,与夹具数据不具有该列,因为它在数据库中更改前被抓获.
在迁移数据库时,移动我的灯具的最佳方法是什么?
我有以下型号:
class Company < ActiveRecord::Base
has_and_belongs_to_many :regions
class Region < ActiveRecord::Base
has_many :requests
has_and_belongs_to_many :companies
class RequestForProposals < ActiveRecord::Base
belongs_to :region
Run Code Online (Sandbox Code Playgroud)
每当我收到新请求时,我都会向同一地区的公司发送通知.
如何在我的灯具中进行设置,以便我可以单独测试找到合适公司的逻辑?
我试过了
region_ids: 1, 2
regions: one, two
Run Code Online (Sandbox Code Playgroud)
在companies.yml中,但都不适用于为公司分配区域.
以下是SQL生成的要点:https://gist.github.com/2713518
假设我在User.rb中有以下代码的User模型:
before_create :create_dependencies
after_create :build_inbox
Run Code Online (Sandbox Code Playgroud)
我还有一个users.yml文件,其中定义了一堆用户装置.
当我运行rake db:fixtures:load时,它似乎不会触发回调.
我已经定义了一些fixtures在doctrine.
当我尝试使用此运行
php app/console doctrine:fixtures:load 然后它要求我清除数据库.
是否可以在不清除数据库的情况下加载它.
我remeber Django已经fixtures可以在单独的表中被加载,而不需要清洁现有的数据库
如本文所述,我在fixture中使用自动关联.例如,如果某个区域对象具有国家/地区ID,而不是执行"country_id":1,则执行"country":"USA"."USA"是我的countries.yml文件中的标签,因此灯具知道如何处理这个问题.但是,这仅在您没有为countries对象指定ID值时才有效.所以我不能将美国的ID指定为1.但如果我不将它指定为1,它最终会成为一些大值8974343 ...这有点奇怪.有没有办法让灯具自动生成不是超高的id?....或者这样可以吗?
与此处描述的问题类似:http: //rpheath.com/posts/411-how-to-use-factory-girl-with-rspec
简而言之(缩短代码):
spec_helper:
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
Run Code Online (Sandbox Code Playgroud)
factories.rb:
Factory.define :state do
f.name "NY"
end
Run Code Online (Sandbox Code Playgroud)
在我的规格中
before(:each) do
@static_model = Factory(:state) # with validate uniqueness of state name
end
Run Code Online (Sandbox Code Playgroud)
错误:
重复的条目名称"NY"等.
问题:在每个规范示例之前,不应该rspec清除数据库,因此不会抛出重复的条目错误吗?
我正在努力从attachment_fu升级到carrierwave,因为attachment_fu在rails 3中被破坏了.
没有一个测试能够运行,因为我们有无效的灯具使用attachment_fu的语法来附件文件.
例如,我们有一个Post模型,它有一个PostAttachment.以下是PostAttachment夹具中的数据:
a_image:
post_id: 1
attachment_file: <%= Rails.root>/test/files/test.png
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
ActiveRecord::StatementInvalid: PGError: ERROR: column "attachment_file" of relation "post_attachments" does not exist
LINE 1: INSERT INTO "post_attachments" ("post_id", "attachment_file"...
Run Code Online (Sandbox Code Playgroud)
attachment_file 本来会被attachment_fu选中,它会处理为模型创建attachment_fu附件的所有处理.
有没有办法在灯具中有图像附件,但使用CarrierWave代替?
unit-testing ruby-on-rails fixtures ruby-on-rails-3 carrierwave
有没有办法在运行测试时按特定顺序加载Rails灯具?例如,参加以下课程......
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
Run Code Online (Sandbox Code Playgroud)
Memberships具有数据库级别的外键约束,在创建它们之前需要Users并Groups存在.但是,因为Rails按字母顺序加载fixture,Memberships之前加载Users,并且发生错误,说明关系不存在(正确地说是这样).
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "memberships" violates foreign key constraint
Run Code Online (Sandbox Code Playgroud)
有没有办法在运行测试时加载前加载Users和Groups固定装置Memberships?
我正在学习Ruby on Rails,我想确保我理解灯具和种子数据之间的区别.
我理解的是,fixtures基本上是测试数据,你运行你的测试断言,并且一旦你的测试完成就没有持久性,而种子你在做类似事情时自动放入数据库rake db:seed.
为什么在这种情况下使用种子?只是为了避免写出所有无数的测试断言?对于您知道在应用程序投入生产时需要在数据库中的数据?
(我想,静态数据总是像消息板上的第一个管理员那样?)
fixtures ×10
ruby ×4
unit-testing ×2
activerecord ×1
carrierwave ×1
django ×1
django-south ×1
doctrine-orm ×1
factory-bot ×1
migration ×1
php ×1
pytest ×1
python ×1
python-3.x ×1
rspec ×1
symfony ×1