On Wednesday, February 12, 2014 7:28:17 AM UTC-8, Nicholas Wieland wrote:
>
> Hi everyone, I'm having a very strange problem testing some rake tasks
> with rspec. I think I'm doing something terribly wrong here, and that's why
> rspec is having one of the weirdest behaviours I've ever seen...
>
> Here is one of my rake tasks:
>
> namespace :rivendell do
> desc 'Outputs the current version of Rivendell'
> task version: :environment do
> puts Rivendell::VERSION
> end
> end
>
> And here is my spec (paths are alright):
>
> describe 'Rakefile' do
> before :all do
> Rake.application.rake_require '../tasks/rivendell'
> Rake::Task.define_task :environment
> end
>
> describe 'rivendell::version' do
> # This doesn't appear to work
> let :run_rake_task do
> Rake::Task["rivendell::version"].reenable
> Rake.application.invoke_task "rivendell::version"
> end
> it 'should display the right version' do
> Rivendell.should_receive('VERSION').and_return('0.1')
> run_rake_task
> end
> end
> end
>
> With this version, all tests pass (I invoke it directly with the rspec
> command).
> The problem is, if I change the should_receive call to
> should_receive('sdjkakja') it works as well, this suggests me I'm not
> testing my code but some kind of weird double I've created (no idea how,
> where or when).
> I already posted on ruby-lang, I thought this place would be more
> appropriate.
>
> TIA,
> ngw
>
`Rivendell.should_receive(:VERSION)` will never work because `VERSION` is
not a message `Rivendell` responds to. VERSION is a constant nested under
the `Rivendell` constant. RSpec has support for stubbing constants but it
uses a different API:
https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/mutating-constants
HTH,
Myron
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/b501a409-a41a-42d7-9e6e-136628fc4cf6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.