Code Monkey home page Code Monkey logo

Comments (7)

icy-arctic-fox avatar icy-arctic-fox commented on May 27, 2024

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.

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on May 27, 2024

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).

from spectator.

postmodern avatar postmodern commented on May 27, 2024

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(...).

from spectator.

postmodern avatar postmodern commented on May 27, 2024

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.

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on May 27, 2024

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.

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on May 27, 2024

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.

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on May 27, 2024

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.

from spectator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.