简体   繁体   中英

How to validate presence of an array in Rails with Rspec?

My specs work fine validating login and in that validation I have access to some_array but in the validation for some_array it fails because I don't have access to it. Is there some special thing I need to do to test for arrays?

model

  validates_presence_of :login, :some_array

rspec

  it { should validate_presence_of(:login) }
  it { should validate_presence_of(:some_array) }

Not much info to go on so...

What you need to think is how is the array getting set in the first place? With each of those it { } blocks a brand new WhateverModel is created.

Is Whatever.some_array populated on creation?

it { expect(WhateverModel.some_array).to_not eq([]) }
it { expect(WhateverModel.some_array).to be }

it { expect(WhateverModel.some_array).to be_kind_of Array }

If your "array" is some kind of model relation then there are other matchers for that.

There are tons of matchers you might see something more useful in the docs: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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