Stub by id
nhorton opened this issue · comments
I filed #618 a few minutes ago and realized that my other big need might actually solve the same issue.
Right now, I overuse any_instance
because often the object that I want to stub is an ActiveRecord object, but the instance the code I am testing will run against is not one I can access and stub trivially. A really simple motivating example is ActiveJob - you often pass ID's there and let the job search by ID.
It would be really helpful if there was something like:
MyClass.instance_where(id: 5).expects(:foobar)
I don't care much on the instance_where
syntax. An alternative would be to use matchers like:
record = MyClass.find(27)
MyClass.instance_matching(record).expects(:foobar)
ActiveRecord instances with the same id === each other (and == actually) and you could then use it other ways, like:
String.instance_matching(/my_regex/).expects(:constantize)
If I had that, I could do the "have one instance raise and the others not" needed for #618 too.