LRDesign/rspec-steps — Issue #6: RSpec's mock framework does not work with steps when in before :each
Author: egilburg
Created: 2012-02-19 22:07:06+00:00
State: open

I have an example e.g.:

before :each do
mock :something
end

steps "do something"
it "should do 1" { assert_something }
it "should do 2 { assert_something }
end

Throughout execution of the "do something" steps (which used to be a single long "it") I'm depending on the mock to have been done. But it doesn't work:

undefined method `mock' for #

I tried rewriting the example as such:

steps "do something"
before :each do
mock :something
end

it "should do 1" { assert_something }
it "should do 2 { assert_something }
end

The result, and error message, is the same. Now, if I simply ditch the before :each and do the following:

steps "do something"
it "should do 1" { mock(:something); assert_something }
it "should do 2 { mock(:something); assert_something }
end

The above DOES work, but it's very unDRY as I have many "it" statements. Ideally I'd want my first version, as my mock in question is generic (mocks the logged in user) and I really want it to simply work for every section in every steps.

I tried googling and found this:

http://www.ruby-forum.com/topic/217974

It seems that mock() doesn't work in "before :all", and from what I can guess, "steps" somehow implicitly converts a "before :each" to a "before :all"

I don't really care if "before :each" would run again prior to each "it" block, or only once per "steps" (but correctly making the mockspace objects available throughout each "it" block of the steps) since my mock statement is idempotent (but some people might care, e.g. if they create real records inside a before each). But from my needs, I just want to run the mock statement once and know that the result mocks are working within each "it" statement of the "steps"

Is it possible to achieve?

Thanks