Rspec 测试唯一性/重复值
我正在尝试编写一个 rspec 测试来检测不同的身份是否具有相同的值(检查提供者和 id 的唯一性)
这是我正在进行的测试。我只是在其中扔了一些废话,因为我变得绝望......
context "no duplicate values should exist" do
identity1 = subject { Factory.create(:valid_identity) }
it { should be_valid }
identity2 = identity1
it { should validate_uniqueness_of(:id) }
it { should have(1).error_on(:id) }
it { should validate_uniqueness_of(:provider) }
it { should have(1).error_on(:provider) }
end
为了让您了解我习惯的结构,我编写了如下所示的基本测试,如果可能的话,我想坚持使用
context "when created without a name" do
subject { Brand.create Factory.build(:valid_brand, :name => nil).attributes }
it { should be_invalid }
it { should have(1).error_on(:name) }
specify { subject.errors[:name].should include "can't be blank" }
end
我的身份工厂相同的结构:
Factory.define :valid_identity, :class => Identity do |identity|
identity.participant {|participant| participant.association(:valid_participant) }
identity.provider "twitter"
identity.extid '11111'
end
感谢任何帮助!
I'm trying to write an rspec test that will detect if different identities have the same values (check uniqueness for provider and id)
Here is the test I'm working on. I just kind of threw some crap into it cause I was getting desperate...
context "no duplicate values should exist" do
identity1 = subject { Factory.create(:valid_identity) }
it { should be_valid }
identity2 = identity1
it { should validate_uniqueness_of(:id) }
it { should have(1).error_on(:id) }
it { should validate_uniqueness_of(:provider) }
it { should have(1).error_on(:provider) }
end
To give you an idea of the structure I'm used to, I've written basic tests like the one below and if possible I'd like to stick to the same sort of structure
context "when created without a name" do
subject { Brand.create Factory.build(:valid_brand, :name => nil).attributes }
it { should be_invalid }
it { should have(1).error_on(:name) }
specify { subject.errors[:name].should include "can't be blank" }
end
my identity factory is as such :
Factory.define :valid_identity, :class => Identity do |identity|
identity.participant {|participant| participant.association(:valid_participant) }
identity.provider "twitter"
identity.extid '11111'
end
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要的行为是:当我创建一个新对象并且数据库中已经有一个具有相同提供者/id 的对象时,新对象必须无效。
在伪代码中,这意味着:
如果您想测试验证,请始终从有效对象开始并更改您想要无效的内容。这样您就可以确定您的测试将来不会失败。
The behaviour you want is: when I create a new object and I already have an object in the database with the same provider/id than must the new object be invalid.
In pseudo code this means:
If you want to test validations, always start from a valid object and change what you want to be invalid. That way you're sure your test won't fail in the future.