Mocks on Constants
Reported by Matt lins | July 24th, 2008 @ 05:00 PM | in No-Milestone-Assigned
I'm trying to mock a Class definition(a constant). This is the most simple repo I could come up with:
https://gist.github.com/2165/bda...
When ran:
------------------------------------------------------------
- spec main_spec.rb
FF
1)
Spec::Mocks::MockExpectationError in 'main should start blah'
Mock 'new#' received unexpected message :new with (no args)
./main.rb:4:in `run'
./main_spec.rb:13:
2)
Spec::Mocks::MockExpectationError in 'main should start blah'
Mock 'new#' received unexpected message :new with (no args)
./main.rb:4:in `run'
./main_spec.rb:18:
Finished in 0.008897 seconds
2 examples, 2 failures
------------------------------------------------------------
However, this works with flexmock:
https://gist.github.com/2165/0fc...
When ran:
------------------------------------------------------------
- spec main_spec.rb
..
Finished in 0.025756 seconds
2 examples, 0 failures
------------------------------------------------------------
I tried to dig around, but the built in mock framework is a little over my head.
Comments and changes to this ticket
-
Kerry Buckley July 24th, 2008 @ 05:21 PM
You need to stub the constructor on the class, but the class will have to be defined. Not sure what your after block was trying to do, but this works:
I'm nothing to do with the rspec project, but I suspect this question probably belongs on the mailing list, not as a ticket.
-
Matt lins July 24th, 2008 @ 05:39 PM
I didn't put it on the mailing list because I feel this is a bug. I don't
think I should need to define the class. Isn't that the point of a mock?
Also, the after block undefines the constant since the before block is
called multiple times. Constants don't like to be defined more than once.
In this simple example a before(:all) would probably work, but in my project
I need to redefine the mock for different specs.
-
Matt lins July 24th, 2008 @ 05:40 PM
I didn't put it on the mailing list because I feel this is a bug. I don't
think I should need to define the class. Isn't that the point of a mock?
Also, the after block undefines the constant since the before block is
called multiple times. Constants don't like to be defined more than once.
In this simple example a before(:all) would probably work, but in my project
I need to redefine the mock for different specs.
-
Matt lins July 24th, 2008 @ 05:49 PM
Sorry for the dup reply. I didn't think my email reply worked, but it did!
-
Pat Maddox July 25th, 2008 @ 01:19 AM
- → State changed from new to invalid
Hi Matt,
In RSpec, mock()'s first argument is the mock's name, and the return values follow. So you want to change your mock lines to be something like:
@blah = mock("blah", :start => true, :stop => true)
Blah = mock("Blah class", :new => @blah)
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Behaviour Driven Development for Ruby.
