简体   繁体   English

Rspec的“ on::update”测试验证不起作用(Rails4 / Rspec 3)

[英]Rspec Test validations of 'on: : update' not working (Rails4/Rspec 3)

I have a model where I implemented a validate to prevent change after the initial value has been set (during the object creation). 我有一个模型,我在其中执行了一个validate来防止在设置初始值后(对象创建期间)更改。

It works this way: 它是这样工作的:

models/deal.rb 车型/ deal.rb

validate :quantity_not_changeable, on: :update

def quantity_not_changeable
      if quantity_changed?
        errors.add(:quantity,
         "Change of the initially defined qty is not possible")
      end
    end

This works in my app: I created a new Deal, it works. 这在我的应用中有效:我创建了一个新交易,它可以正常工作。 I try to edit it by changing the 'quantity' field, it fails. 我尝试通过更改“数量”字段进行编辑,但失败了。 I try to edit another field (apart from 'quantity'), it works. 我尝试编辑另一个字段(“数量”除外),它可以工作。 so all is working. 所以一切正常。

But my Rspec Test to test it FAILS. 但是我的Rspec测试失败了。

Actually I know it does NOT work because there is a problem with on: : validate. 实际上, 我知道它不起作用,因为on::验证存在问题。

Indeed WITHOUT the 'on: : validate', the test passes, but of course I can't just remove 'on: :update', as I only want the user not to be able to edit it after the initial creation of the Deal). 实际上,没有'on::validate',测试就通过了,但是我当然不能只删除'on :: update',因为我只希望用户在最初创建交易后不能对其进行编辑)。

describe Deal do

  let(:admin_user) { FactoryGirl.create(:admin_user) }

  before(:each) do
    @attr = {
      title:                              "Neque porro quisquam est qui dolorem",
      description:                        "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum",
      quantity:  10
    }
  end

describe "my test" do

 describe "test that nb of quantity can't be changed after initial creation" do    
    it "fails if initial quantity is changed" do 
      deal = Deal.create(@attr)
      deal.update(quantity: 15)     
      expect(deal).to have(1).errors_on(:quantity)

    end
  end

but I get this rspec error: 但我收到此rspec错误:

Failure/Error: expect(deal).to have(1).errors_on(:quantity)
       expected 1 errors on :quantity, got 0

I am going to use below "print variable": p Deal to show you WHY I am sure it is due to on: :update which is not working in TESTS. 我将在“打印变量”下面使用:p交易以向您显示为什么我确定这是由于::update在TESTS中不起作用。

FIRST: if I place 'p deal' just before updating it, let's see the value of the deal object: 首先:如果我在更新之前放置“ p deal”,让我们看看deal对象的值:

describe "my test" do

 describe "test that nb of quantity can't be changed after initial creation" do    
    it "fails if initial quantity is changed" do 
      deal = Deal.create(@attr)
      p deal
      deal.update(quantity: 15)     
      expect(deal).to have(1).errors_on(:quantity)

    end
  end

I get the following output (we see that all is normal: quantity is indeed 10) 我得到以下输出(我们看到一切正常:数量确实为10)

#<title: "Neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 10>

SECOND: now let's move the 'p deal' AFTER my attempt in the test to update the value of 'quantity' 第二:现在让我们在测试中尝试更新“数量”的值后移动“ P交易”

describe "my test" do

 describe "test that nb of quantity can't be changed after initial creation" do    
    it "fails if initial quantity is changed" do 
      deal = Deal.create(@attr)
      deal.update(quantity: 15)
      p deal     
      expect(deal).to have(1).errors_on(:quantity)

    end
  end

Now the output is:( we see that CONTRARY to what we could expect due to the validate on: :update in the code, the Rspec test does manage unfortunately to update the value of quantity ): 现在的输出是:( 由于代码中对:: update的验证,我们看到了与预期相反的结果,不幸的是Rspec测试确实设法更新了数量的值 ):

#<title: "Neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 15>

So I don't understand why in reality, I tested manually and can not update, but in my rspec test suite it does update!!! 所以我不明白为什么实际上我是手动测试并且无法更新,但是在我的rspec测试套件中确实会更新!

Hours to understand it but to no success...I tried to find some online resources and some say on: :update is a weak way to do things . 花费数小时来理解它,但没有成功...我试图找到一些在线资源,并在上面说::update是一种做事的弱方法 I really don't know what to do with this weird bug. 我真的不知道该怎么处理这个奇怪的错误。

Please help. 请帮忙。

I guess entry isn't saved when called create method because of other validation. 我猜由于其他验证,在调用create方法时不会保存条目。 You can try open up a console with rails c and evaluate following: 您可以尝试打开带有rails c的控制台并评估以下内容:

deal = Deal.create(title: 'mytitle', description: 'mydescription', quantity: 10)
deal.persisted?

then if you got false in order to find out what errors were when creating: 然后,如果您为了得到错误而发现错误,请执行以下操作:

deal.errors.messages

EDIT 编辑

This test should passes: 该测试应通过:

it "fails if initial quantity is changed" do 
  deal = Deal.create(@attr)
  deal.update(quantity: 26)     
  expect(deal.errors.messages).to eq({quantity: ["Change of the initially defined qty is not possible"]})
end

But these should not: 但是这些不应该:

it "exploited test that should not pass because messages hash should not be empty" do 
  deal = Deal.create(@attr)
  deal.update(quantity: 26)     
  expect(deal.errors.messages.empty?).to eq(true)
end

it "exploited test in other variation" do 
  deal = Deal.create(@attr)
  deal.update(quantity: 26)     
  expect(deal.errors.messages).to eq({})
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM