Stubbing constants in Crystal spec

Hello,

I want to stub the constant from a module in the spec. How can I do that? I would want to do it within the describe block.
I see that there is no “before do” or “stub_const” thing which crystal permits.

Thanks in advance.
Nitika

I think it needs a bit of work, but this shard https://github.com/waterlink/mocks.cr let’s you do stubs.

Also, you can use Spec.before_each https://crystal-lang.org/api/0.35.1/Spec.html#before_each(&block)-class-method if you need to run callbacks in specs.

I think you would be better off with refactoring things to make it so you don’t need to do that. Constants by nature are something that shouldn’t need mocked since it’s, you know, constant.

I’d be happy to take a look if you have some code to share.

yeah, after having spent years mocking things I’ve determined that if you “need” to mock something it usually means you didn’t design your code properly. And if you end up having to maintain that code a year from now it will be a pain. Save yourself some future pain and design your code in a way that avoids the need to mock things.

I’ve found the “Humble Object” design pattern to be most helpful.

Thanks for your responses on this. I also noticed that there was a way to avoid the “need” to stub constants and it worked fine.