0

Is it possible to validate the actual parameter received by a mock objects method parameter? I cannot do a direct comparison because i'm using Fabricate and converting the object into a serialised format.

for example:

expect(user).to have_received(:add).with(valid_user())

so in that case valid_user() would accept the parameter, validate and return a boolean, to verify a valid value was passed into user.add()

can something like that be done?

So far I've been reading the documentation in https://github.com/rspec/rspec-mocks regarding Argument Matchers

Edit: in my specific case, the argument is a quite large string. I would like to validate that the string is valid by potentially running a regex against the string. So i would like to run the argument through a validation method which simply returns true/false.

Cœur
  • 37,241
  • 25
  • 195
  • 267
wired00
  • 13,930
  • 7
  • 70
  • 73
  • actually, I think what I need in my case is something like: `expect(user).to have_received(:add).with(/regexcheck/)`, i can use the regex check to validate the only argument accepted – wired00 Sep 20 '15 at 02:57

1 Answers1

1

You can use a custom matches in with() as shown in the spec docs https://relishapp.com/rspec/rspec-mocks/docs/setting-constraints/matching-arguments#using-a-custom-matcher

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78