3

Still learning Rspec as a whole, so thanks for patience.

Return value is this:

{ supermodel: { 'id': 1, 'name': 'J', 'model_attributes': [ {attr1: 'T', attrA: 1}, {attr2: 'F', attrB: 2}, {attr3: 'T', attrC: 3} ], } } 

Trying to get an expectation that says that a hash key named 'model_attributes' contains a value of an array that includes the following key value pairs - {attr2:F, attrB: 2} and {attr3: T, attrC: 3}.

Any insight would be welcome.

6
  • your Result of method is this.. is invalid Ruby object, double check it ;) Commented Mar 7, 2017 at 20:05
  • Maybe this helps? apidock.com/rspec/Spec/Mocks/ArgumentMatchers/hash_including Commented Mar 7, 2017 at 20:07
  • True @AndreyDeineko reformatted the assertion to be more ruby. Thanks! Commented Mar 7, 2017 at 20:14
  • @JoaWa i meant, that 'model_attributes': [ {attr1: T, attrA: 1}, {attr2: F, attrB: 2}, {attr3: T, attrC: 3} ] is not a valid object in Ruby. It looks almost like a hash, but it lacks braces and still would be invalid because of T, F - these will throw an Uninitialized constant T Commented Mar 7, 2017 at 20:15
  • Sorry for the lack of clarity @AndreyDeineko. Provided the big picture return sample. Was aiming for brevity, but lacked in accuracy. Thanks for input. Commented Mar 7, 2017 at 20:29

3 Answers 3

3
describe 'Stuff' do let(:model_attributes) do [ {attr1: 'T', attrA: 1}, {attr2: 'F', attrB: 2}, {attr3: 'T', attrC: 3} ] end let(:result) do { supermodel: { 'id': 1, 'name': 'J', 'model_attributes': model_attributes } } end it 'has the correct model_attributes value' do expect(result.dig(:supermodel, :model_attributes)).to eq(model_attributes) end end 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Andrey - ended up borrowing this strategy.
2

Try

describe 'ModelAttributes' do let(:model_attributes) { [{ attr1: 'T', attrA: 1 }, { attr2: 'F', attrB: 2 }, { attr3: 'T', attrC: 3 }] } it 'includes the required attributes' do expect(model_attributes).to include({ attr2:'F', attrB: 2 }, { attr3: 'T', attrC: 3 }) end end 

Comments

1

In retrospect this is alot easier now. Simply traversing the hash and expecting the values was the easiest way. Refactoring ended me up in doing this:

supermodel[:model_attributes].each do |attr| expect(attr[:attrB])to be 2 end 

Thanks for all the suggestions, led me down the right path.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.