Skip to content

RSpec's mock framework does not work with steps when in before :each #6

@egilburg

Description

@egilburg

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 #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x0000010248f2a8>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions