vendredi 16 novembre 2018

RSpec: assert that mocks are correct

When writing RSpec tests, I often find myself mocking various functions in the code base. However, there doesn't seem to be anything in place to prevent me from accidentally mocking the functions incorrectly, and I would like to enforce this somehow.

For a concrete example, let's say I have the function I'm testing:

def some_function(a:, b:, c:)
  { a: a, b: [b, b, b], c: { a: 1, b: 2, c: 3 } }
end

And a test for it

it 'should return the correct result' do
  subject = some_function(a: 1, b: 2, c: 3)
  expect(some_function).to_return(1)
  expect(subject).to eq(1)
end

I completely changed the interface of the function, but my test still passed. I love how straightforward it is to write mocks, but it would be really great to be able to enforce that the mocks I'm writing are following the interface provided by the function. Is there any way to accomplish this? I'm ok with including examples, or shared contexts, or what not, as long it accomplishes what I need.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire