title: Rails RSpec Mocking Date.today.wday in Rake Task
body: <p>I'm trying to mock Date.today.wday in a rake task in rspec. </p>
  
  <p>Gem versions: RSpec 2.14.8 --- Rails 4.1.1 --- ruby 2.0.0</p>
  
  <p>Here is a simplified fake version of my test to illustrate essentially what I'm trying to do:</p>
  
  <pre><code>describe "scheduler" do
    describe ":thursday_invitations" do
      let(:run_issue_invites) do
        Rake::Task[:thursday_invitations].reenable
        Rake.application.invoke_task :thursday_invitations
      end
  
      before do
        Rake.application.rake_require 'tasks/scheduler'
        Rake::Task.define_task(:environment)
        Date.today.should_receive(:wday).and_return(4) ###MY NEMESIS CODE LINE
      end
  
      context "on thursday" do
        it "issues invitations" do
          expect(Date.today.wday).to eq(4) ###THE VERIFICATION TEST THAT KEEPS FAILING
          run_issue_invites
          expect(&lt;other_stuff_to_test&gt;).to &lt;return_properly&gt;
        end
      end
    end
  end
  </code></pre>
  
  <p>So, the real key of this is mocking out the Date.today.wday. Because I want to be able to run my spec on any day of the week, I need to mock/stub out this method to always return "4" (the day-number for Thursday in Rails). So, I initially setup my test to first verify that it is receiving a "4" (the assertion in the test). If today is, say, Friday (which it is) and I run the test, it fails saying that it expected "4" but got "5". That is, it is not returning the value that I want it to when I receive the method. I have tried stubbing with similar ineffective results. Normally, mocking is a breeze, but what seems to be the hangup is <code>.wday</code> which operates on <code>Date.today</code>. </p>
  
  <p>Because this is a rake task (which I'm not as familiar with mocking), I may have to specify something further, but I haven't been able to get to the bottom of it...</p>
  
  <p>Let me know if you need any other clarifying information.</p>