rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.16k stars 1.03k forks source link

Stubbing method with block throws "Illegal Instruction: 4" on OS X 10.7.5 #828

Closed ericgriffis closed 10 years ago

ericgriffis commented 10 years ago

When I attempt to stub a method using a block to generate a return value, rspec prints "Illegal Instruction: 4" and exits. According to the second answer to this question, this means that some binary instruction is invalid on the executing OS (I'm running rspec-rails 2.13.1 on OS X 10.7.5).

Has anyone else noticed this problem? Is this a problem internal to rspec, rspec-rails, or rails, or could it be something else I have installed?

JonRowe commented 10 years ago

Can you provide a code sample to reproduce this? And we'd need you to be running the latest version of RSpec (2.14) and let us know what version of Ruby you're running.

ericgriffis commented 10 years ago

I am running rpsec-rails 2.13.1 and ruby 1.9.3p392, sorry for leaving that out before.

In attempting to find the simplest code that reproduces the error, I noticed a strange contingency on this behavior.

With the following code, rspec will print one failure then "Illegal instruction: 4" and exit:

describe "test" do
  before :each do
    Listing.stub(:new).and_return do
      FactoryGirl.build(:listing)
    end
  end

  describe "stub" do
    it "should do foo" do
      @listing = Listing.new
    end
    it "should do bar" do
      @listing = Listing.new
    end
  end
end

Here is the command line output:

FIllegal instruction: 4

However, with the following code, rspec will complete successfully, printing a different error:

describe "test" do
  before :each do
    Listing.stub(:new).and_return do
      FactoryGirl.build(:listing)
    end
  end

  describe "stub" do
    it "should do foo" do
      @listing = Listing.new
    end
  end
end

Here is the command line output for this code:

F

Failures:

  1) test stub should do foo
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/ericgriffis/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/class/attribute.rb:81

Note that the only difference between the two code blocks is that the former runs an identical test on the same (stubbed) method twice.

Thanks for looking into this; it's really got me stumped.

xaviershay commented 10 years ago

First, you need to do this:

listing = FactoryGirl.build(:listing)
Listing.stub(:new).and_return(listing)

What you had was equivalent to...

Object.stub(:new).and_return do
  Object.new
end

... which can't work because new has already been stubbed by the time you call it, hence the SystemStackError.

I haven't repro'd the illegal instruction part yet though.

myronmarston commented 10 years ago

I suspect that the illegal instruction has to do with how @ericgriffis's ruby interpreter was compiled, and we probably won't be able to repro it on our machines.

xaviershay commented 10 years ago

Don't think there's any other action here for us then. @ericgriffis please re-open if you're not satisfied :)

JonRowe commented 10 years ago

Yeah I suspect this is an artefact of the build/new relationship internally and doing something weird in your code / build of ruby. This isn't an RSpec issue.