wired00
wired00

Reputation: 14438

rspec, validate the actual Expectation parameter

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.

Upvotes: 0

Views: 894

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

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

Upvotes: 0

Related Questions