Closed postmodern closed 2 years ago
Since new
is a class method, the stub name should have a self.
prefix.
mock TestFoo::TestClass do
stub self.new
end
But... looks like when I implemented the fix for stubbing exit
, a bug was introduced that prevents this from working.
Should be resolved in v0.9.40, can you verify?
Needs to be mocked by using stub self.new
(the self.
prefix is required since .new
is a class method).
Now I'm curious how one would stub self.new
, but have it return a copy of the class? This is to to ensure that new
is called under the hood by the class-method which one is testing, and to control the value new
returns so one can expect(instance).to receive(...)
.
Also having trouble stubbing File.file?
.
mock File do
stub self.file?, path : String, return_type: Bool
end
before_each do
expect(File).to receive(:file?).with("/etc/fedora-release").and_return(true)
end
it { expect(subject.linux_distro).to be(CommandKit::OS::Linux::LinuxDistro::Fedora) }
1) CommandKit::OS::Linux.linux_distro and the /etc/fedora-release file exists subject.linux_distro is CommandKit::OS::Linux::LinuxDistro::Fedora
Failure: File did not receive #file?("/etc/fedora-release") : Bool at spec/os/linux_spec.cr:22 at least once with any arguments
expected: At least once with any arguments
received: 0 time(s)
I think that error message is slightly misleading, since I'm not expecting the #file?
instance method, but the class-method.
The error is misleading, I think it's hard-coded to prefix stubbed method names with #
despite it being a class method.
Can you try this code with latest master
branch? There was a fix made the other day that might address it.
This functionality has been verified to work in the overhauled mock system. See the spec related to this issue for its implementation. This is coming in v0.11 and is currently available on master for testing.
Note that this uses inject_mock
, which is modify the existing types. They should behave the same as before, but their underlying code will have stub features added. This results in different compiled code for the type between test and non-test.
This issue should be resolved in v0.11. There is a spec dedicated to it. If your issue isn't resolved, please reopen this issue.
Ran into this when porting RSpec tests which stubs the
new
method so that the tests control the instance object that's returned.