Code Monkey home page Code Monkey logo

Comments (8)

icy-arctic-fox avatar icy-arctic-fox commented on May 24, 2024 1

Heads up, this is changing in v0.11. Instead of relying on stubs, exit will raise a Spectator::SystemExit error. This mimics what RSpec does. See the related spec for this issue for an example.

from spectator.

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

It seems that Spectator isn't stubbing the exit method. There needs to be a stub defined prior to calling receive(:exit). For instance:

class SomeClass
  def goodbye
    exit
  end
end

mock SomeClass do
  stub exit
end

it "captures exit" do
  expect(subject).to receive(:exit).with(0)
  subject.goodbye
end

This code doesn't work though, since exit isn't defined in SomeClass - it's a top-level method. Another issue is that a type is required for the mock block, but there isn't one for a top-level method. (maybe Object?)

Do you have any code that shows this in action in RSpec?

from spectator.

postmodern avatar postmodern commented on May 24, 2024

Code: https://github.com/postmodern/command_kit/blob/a06a131ddde42fa7f7730debbdc66136282bae1f/lib/command_kit/main.rb#L46-L56
RSpec specs: https://github.com/postmodern/command_kit/blob/a06a131ddde42fa7f7730debbdc66136282bae1f/spec/main_spec.rb#L28-L100

from spectator.

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

This should be possible in v0.9.39 - Can you try it out and confirm?

The following spec works:

require "../spec_helper"

Spectator.describe "GitHub Issue #29" do
  class SomeClass
    def goodbye
      exit 0
    end
  end

  mock SomeClass do
    stub exit(code)
  end

  describe SomeClass do
    it "captures exit" do
      expect(subject).to receive(:exit).with(0)
      subject.goodbye
    end
  end
end

from spectator.

postmodern avatar postmodern commented on May 24, 2024

Ah, stubbing/capturing exit now works on instances, but not at the class-level. I'm guessing mock/stub is assuming exit is an instance method?

require "./spec_helper"

Spectator.describe "test exit" do
  class Foo
    def self.test
      exit 0
    end
  end

  mock Foo do
    stub exit(code)
  end

  subject { Foo }

  it "must capture exit" do
    expect(subject).to receive(:exit).with(0)

    subject.test
  end
end
$ crystal spec spec/test_spec.cr 
test exit

from spectator.

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

I think I see why this is happening. I'll try to get a fix in.

from spectator.

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

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

For class methods that call exit, it will need to stub self.exit(code).

from spectator.

postmodern avatar postmodern commented on May 24, 2024

Confirmed self.exit(code) works.

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.