GithubHelp home page GithubHelp logo

Comments (9)

icy-arctic-fox avatar icy-arctic-fox commented on June 15, 2024

Is Test mocked? I added a mock block and it started working.

Spectator.describe Test do
  mock Test do
    stub method2
  end

  describe "#method1" do
    it do
      expect(subject).to receive(:method2)

      subject.method1
    end
  end
end

from spectator.

postmodern avatar postmodern commented on June 15, 2024

hmmm stubbing one or both methods doesn't seem to be working:

  describe "#~" do
    mock Chars::CharSet do
      stub :===
    end

    let(other) { "foo" }

    it "must call #===" do
      expect(subject).to receive(:===).with(other)

      subject =~ other
    end
  end
  1) Chars::CharSet#~ must call #===
     Failure: subject did not receive #===("foo") : Nil at spec/char_set_spec.cr:770 at least once with any arguments

       expected: At least once with any arguments
       received: 0 time(s)

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on June 15, 2024

Should be resolved in v0.9.40, can you verify?

from spectator.

postmodern avatar postmodern commented on June 15, 2024

Confirmed it works on the original Test example.

Although having trouble with my Chars::CharSet example.

  describe "#~" do
    mock Chars::CharSet do
      stub :===
    end

    let(other) { "foo" }

    it "must call #===" do
      expect(subject).to receive(:===).with(other)

      subject =~ other
    end
  end

Produces:

  1) Chars::CharSet#~ must call #===
     Failure: subject did not receive #===("foo") : Nil at spec/char_set_spec.cr:770 at least once with any arguments

       expected: At least once with any arguments
       received: 0 time(s)

from spectator.

postmodern avatar postmodern commented on June 15, 2024

Changing the stub syntax to stub ===(other) causes another odd compilation error:

In spec/char_set_spec.cr:6:9

 6 | let(char_range) { 'A'..'Z' }
           ^---
Error: wrong number of arguments for macro 'stub' (given 0, expected )

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on June 15, 2024

There's a combination of things going on here. Because of some syntactical things, === must be a symbol on the stub line. The arguments also have to be defined slightly different. The following works for me:

  class Matcher
    def =~(other)
      self === other
    end

    def ===(other)
      true
    end
  end

  describe Matcher do
    mock Matcher do
      stub :===, other
    end

    describe "#~" do

      let(other) { "foo" }

      it "must call #===" do
        expect(subject).to receive(:===).with(other).and_return(true)

        subject =~ other
      end
    end
  end

I had to add and_return(true) to the expect line because it returns nil if that clause isn't provided.

from spectator.

postmodern avatar postmodern commented on June 15, 2024

Hmm getting a weird error:

  describe "#~" do
    mock Chars::CharSet do
      stub :===, other
    end

    let(other) { "foo" }
    let(return_value) { true }

    it "must call #===" do
      expect(subject).to receive(:===).with(other).and_return(return_value)

      expect(subject =~ other).to be(return_value)
    end
  end
There was a problem expanding macro 'stub'

Code in spec/char_set_spec.cr:6:9

 6 | let(char_range) { 'A'..'Z' }
           ^
Called macro defined in lib/spectator/src/spectator/mocks/stubs.cr:3:13

 3 | private macro stub(definition, *types, _file = __FILE__, _line = __LINE__, return_type = :undefined, &block)

Which expanded to:

 >  9 | __temp_438.mocks.record_call(self, __temp_440)
 > 10 | if (__temp_441 = __temp_438.mocks.find_stub(self, __temp_440))
 > 11 |   return __temp_441.call!(__temp_439) { previous_def }
                                                ^-----------
Error: there is no previous definition of '==='

from spectator.

icy-arctic-fox avatar icy-arctic-fox commented on June 15, 2024

I'm not sure why that's happening. I've tried (what I think you're working on) with this snippet and it works:

  describe "#~" do
    mock Chars::CharSet do
      stub :===, other : String
    end

    let(other) { "foo" }
    let(return_value) { true }

    it "must call #===" do
      expect(subject).to receive(:===).with(other).and_return(return_value)

      expect(subject =~ other).to be(return_value)
    end
  end

I added : String type restriction to match the original definition.

But with the code you posted, it compiles, but fails since the method isn't stubbed right (needs the String type restriction on other). Perhaps you can send along the code if this snippet doesn't work with what you have.

Sorry about all of this back and forth. The mock support in Spectator could certainly use some improvements.

https://gist.github.com/icy-arctic-fox/63e500c462107cf29ce1c6fb1d3b652c

from spectator.

postmodern avatar postmodern commented on June 15, 2024

Aaaaah the : String constraint fixed it. Thanks!

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.