Rspec 测试唯一性/重复值

发布于 2024-12-14 12:04:56 字数 1082 浏览 0 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2024-12-21 12:04:56

您想要的行为是:当我创建一个新对象并且数据库中已经有一个具有相同提供者/id 的对象时,新对象必须无效。

在伪代码中,这意味着:

# 1. create a totally valid object and save it in the database
obj1 = create_valid_object_and_save_it
# 2. we need a second object that's totally valid when obj1 exists
obj2 = create_second_valid_object_dont_save_it
# 3. we make the bad condition
obj2.provider = obj1.provider
# 4. now comes the actualy test, obj2 must not be valid!
assert ! obj2.valid?

如果您想测试验证,请始终从有效对象开始并更改您想要无效的内容。这样您就可以确定您的测试将来不会失败。

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:

# 1. create a totally valid object and save it in the database
obj1 = create_valid_object_and_save_it
# 2. we need a second object that's totally valid when obj1 exists
obj2 = create_second_valid_object_dont_save_it
# 3. we make the bad condition
obj2.provider = obj1.provider
# 4. now comes the actualy test, obj2 must not be valid!
assert ! obj2.valid?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文