On Thu, Jan 24, 2013 at 11:04 AM, John Julien <[email protected]> wrote:
> I worded that wrong, sorry for the confusion. I am actually trying to
> test for what a function is called with NOT what it returns. "execute" is
> called with an Array that contains a system command and arguments. I want
> to make sure that the array contains at least "/usr/sbin/usermod" and "-e".
> If all I cared about was making sure the /usr/sbin/usermod was in the
> array this syntax works
> "provider.expects(:execute).with(includes('/usr/sbin/usermod'))"
>
> The Mocha documentation uses this example of the includes method:
> object.expects(:method_1).with(includes('foo'))
>
> Your suggestion of using the block method appears to be exactly what I
> needed
> provider.expects(:execute).with() {|args|
> args.include?('/usr/sbin/usermod') and args.include?('-e') }
>
>
The other way of writing that is:
provider.expects(:execute).with(all_of(includes('/usr/sbin/usermod'),
includes('-e')))
It would be nice if includes took multiple items, but it doesn't look like
it does. You can see the full list of matchers you can use in the mocha
docs at http://gofreerange.com/mocha/docs/Mocha/ParameterMatchers.html
> Thanks!
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On
> Behalf Of Daniel Pittman
> Sent: Thursday, January 24, 2013 12:10 PM
> To: [email protected]
> Subject: Re: [Puppet-dev] Rspec Help
>
> On Wed, Jan 23, 2013 at 9:35 PM, John Julien <[email protected]>
> wrote:
> > I'm trying to write a test case that expects an array to come back
> > from a function and I want to make sure at least 2 elements exist
> > "/usr/sbin/usermod" and "-e"
> >
> > My code is:
> > provider.expects(:execute).with(includes('/usr/sbin/usermod'),
> > includes('-e'))
>
> That isn't valid syntax, and I can't even quite guess what you are trying
> to do:
>
> Calling `.with` using arguments means "expect the function to be invoked
> with *exactly* these arguments".
>
> If you want to check only the first two, you need to use the block version
> instead.
>
> You are also calling the top level `include` function there, which is
> unlikely to work. You can only invoke matchers after a `.should` or
> equivalent is applied to them.
>
>
> That also doesn't seem to be testing what you say you are testing: you
> said you wanted to test "an array to come back from a function", but your
> example is testing the arguments that a function is called with.
>
> Generally speaking, the former is a much better test. Testing what the
> function returns is often much less tied to the implementation details than
> testing how a different function is called (presumably with the results of
> your own function.)
>
> --
> Daniel Pittman
> ⎋ Puppet Labs Developer – http://puppetlabs.com ♲ Made with 100 percent
> post-consumer electrons
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> Visit this group at http://groups.google.com/group/puppet-dev?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> Visit this group at http://groups.google.com/group/puppet-dev?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/puppet-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.