samedi 17 février 2018

How to test assignment of a Proc via Rspec?

I'm using the DataMapper gem.

I have some code that dynamically creates tables based on certain rules. So I have a line of code like this:

def set_table_name(table_name:)
  adapter = DataMapper.repository(:default).adapter
  adapter.resource_naming_convention = lambda { |_| table_name }
end

This comes straight from their documentation: http://www.rubydoc.info/github/datamapper/dm-core/DataMapper/NamingConventions

This works just fine, but I want to write a spec to know that it's setting the naming convention correctly. If it wasn't a lambda but just setting the value, then I'd probably do:

# assume 'adapter' is already set in the test, either as a mock or whatever
expect(adapter).to receive(:resource_naming_convention=).with("some_value")
something.set_table_name(table_name: "some_value")

Since DataMapper is an external dependency, I don't know what side-effects or requirements the setter has, so I just want to verify that it's getting called correctly.

However, since it's being called with a lambda, I'm not sure how to do this. I can do this:

# assume 'adapter' is already set in the test, either as a mock or whatever
expect(adapter).to receive(:resource_naming_convention=).with(kind_of(Proc))
something.set_table_name(table_name: "some_value")

But I'd really like to validate that "some_value" is the execution value of the Proc.

How do I do that?

Aucun commentaire:

Enregistrer un commentaire