Forum: RSpec Mock should receive no messages?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2009-06-03 19:46
Hello all,
Is there a way to explicitly tell a mock to expect no messages and give
an error if it does? I believe this is the default behavior, but thought
it might be nice for code readers to see.

Thank you!
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-06-03 20:09
(Received via mailing list)
On Wed, Jun 3, 2009 at 12:46 PM, Sebastian W. <lists@ruby-forum.com>
wrote:
> Hello all,
> Is there a way to explicitly tell a mock to expect no messages and give
> an error if it does? I believe this is the default behavior, but thought
> it might be nice for code readers to see.

You can tell it to expect not to receive a specific message, but there
is no way to say that it should not receive any messages.

Cheers,
David
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2009-06-03 20:19
David Chelimsky wrote:
> On Wed, Jun 3, 2009 at 12:46 PM, Sebastian W. <lists@ruby-forum.com>
> wrote:
>> Hello all,
>> Is there a way to explicitly tell a mock to expect no messages and give
>> an error if it does? I believe this is the default behavior, but thought
>> it might be nice for code readers to see.
>
> You can tell it to expect not to receive a specific message, but there
> is no way to say that it should not receive any messages.
>
> Cheers,
> David

Okay, cool - feature request? : )
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-06-03 20:29
(Received via mailing list)
On Wed, Jun 3, 2009 at 2:01 PM, David Chelimsky <dchelimsky@gmail.com>
wrote:
> On Wed, Jun 3, 2009 at 12:46 PM, Sebastian W. <lists@ruby-forum.com> wrote:
>> Hello all,
>> Is there a way to explicitly tell a mock to expect no messages and give
>> an error if it does? I believe this is the default behavior, but thought
>> it might be nice for code readers to see.
>
> You can tell it to expect not to receive a specific message, but there
> is no way to say that it should not receive any messages.

Maybe not explicitly but doesn't isn't making a mock with no
expectations effectively the same thing:

describe "an object which should not get any messages" do
  it "should not receive any messages" do
    o = mock("Object")
    o.foo
  end
end

Mock 'Object' received unexpected message :foo with (no args)


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-06-03 20:34
(Received via mailing list)
On Wed, Jun 3, 2009 at 1:26 PM, Rick DeNatale <rick.denatale@gmail.com>
wrote:
> Maybe not explicitly but doesn't isn't making a mock with no
> expectations effectively the same thing:
>
> describe "an object which should not get any messages" do
>  it "should not receive any messages" do
>    o = mock("Object")
>    o.foo
>  end
> end
>
> Mock 'Object' received unexpected message :foo with (no args)

Right - Sebastian noted that in the original post - but he's looking
for something explicit.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-06-03 20:40
(Received via mailing list)
On Wed, Jun 3, 2009 at 2:30 PM, David Chelimsky <dchelimsky@gmail.com>
wrote:
>>
>> Mock 'Object' received unexpected message :foo with (no args)
>
> Right - Sebastian noted that in the original post - but he's looking
> for something explicit.

Well how about:

describe "an object which should not get any messages" do
  it "should not receive any messages" do
    o = mock("Object")
    o.should_not_receive(:a_damned_thing)
    o.foo
  end
end

<G>


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-06-03 20:44
(Received via mailing list)
On Wed, Jun 3, 2009 at 1:19 PM, Sebastian W. <lists@ruby-forum.com>
wrote:
>>
>> Cheers,
>> David
>
> Okay, cool - feature request? : )

You never have to ask to make a feature request - just make one:

http://rspec.lighthouseapp.com
39100495c9937c39b2e0c704444e1b4a?d=identicon&s=25 Pat Maddox (Guest)
on 2009-06-05 02:55
(Received via mailing list)
On Wed, Jun 3, 2009 at 10:46 AM, Sebastian W. <lists@ruby-forum.com>
wrote:
> Hello all,
> Is there a way to explicitly tell a mock to expect no messages and give
> an error if it does? I believe this is the default behavior, but thought
> it might be nice for code readers to see.

Document it in the mock object's name.

my_mock = mock('never receives a message')

I don't see any reason why something should be added to RSpec core.

Pat
This topic is locked and can not be replied to.