Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

Define stub/mock expectation/should_receive in one line?

Anyone know of a way to shorten this to one line? (RSpec 2)

location = mock
location.should_receive(:build)

For example, you can define the following:

location = stub
location.stub(build: true)

The above is the same as:

location = stub(build :true)

So, anyone see a way to specify an expectation in the mock call?

Upvotes: 1

Views: 447

Answers (3)

megas
megas

Reputation: 21791

If every your test has mock definition, you can shorten the notation by using let in the beginning of file.

let (:location) { double :location }

Then every time when you're using location, it automatically creates new mock object:

location.should_receive(:build)

Upvotes: 1

zetetic
zetetic

Reputation: 47548

location = mock.tap { |loc| loc.should_receive(:build) }

Upvotes: 4

Volodymyr Rudyi
Volodymyr Rudyi

Reputation: 648

Looks ugly, though works:

    (location = mock).should_receive(:build)

Upvotes: 1

Related Questions