0

I can't get my Rake task test to pass; it seems that after the rake task has run I lose my instance variable as it returns nil whereas before the Rake task is run I get a record

So before all my tests I have this config setup

ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require 'rake'
load File.expand_path('../../lib/tasks/create_events.rake', __FILE__)
load File.expand_path('../../lib/tasks/update_event.rake', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.before(:suite) do
   DatabaseCleaner.strategy = :truncation
   DatabaseCleaner.clean_with :truncation
   Rake::Task.define_task(:environment)
   Rake::Task['create_events:create_event_data'].invoke
  end

  config.after(:suite) do
    DatabaseCleaner.clean
  end
end

My spec file is setup like so

require "rails_helper"
require 'rake'

RSpec.describe "update_event:next_event_date Rake Task" do

 it "should change the next occurrence date" do
  @update_event = Event.where(next_occurrence: Date.today)
  # So here @update_event returns 1 record (Active Record Relation)
  Rake::Task.define_task(:environment)
  Rake::Task['update_event:next_event_date'].invoke
  # By here @update_event = nil (Active Record Relation = [])

   case @update_event.first.weekly_frequency
   when 1
    expect(@update_event.first.next_occurrence).to eq(Date.today + 7.days)
   when 2
    expect(@update_event.first.next_occurrence).to eq(Date.today + 14.days)
   when 6
    expect(@update_event.first.next_occurrence).to eq(Date.today + 42.days)
   end
 end
end

What is the reason behind this and how do I test this correctly?

My actual Rake task is this

namespace :update_event do
 desc "Update next_occurrence date for an event"
  task next_event_date: :environment do
   @event = Event.where(next_occurrence: Date.today)
    @event.each do |e|
     if e.weekly_frequency == 1
      e.update_attributes next_occurrence: Date.today + 7.days
     elsif e.weekly_frequency == 2
      e.update_attributes next_occurrence: Date.today + 14.days
     elsif e.weekly_frequency == 6
      e.update_attributes next_occurrence: Date.today + 42.days
     end
   end
  end
 end

Edit

So I have modified my spec slightly to this

require "rails_helper"
require 'rake'

RSpec.describe "update_event:next_event_date Rake Task" do

it "should change the next occurrence date" do
  @update_event = Event.where(next_occurrence: Date.today)
  event_id = Event.find(@update_event.first.id)
  Rake::Task.define_task(:environment)
  Rake::Task['update_event:next_event_date'].invoke
  @after_update = Event.find(event_id)
   case @after_update.weekly_frequency
   when 1
    expect(@after_update.next_occurrence).to eq(Date.today + 7.days)
   when 2
    expect(@after_update.next_occurrence).to eq(Date.today + 14.days)
   when 6
    expect(@after_update.next_occurrence).to eq(Date.today + 42.days)
  end
 end    
end

This passes but I'm not sure why I lose my first instance variable.

1 Answer 1

1

You're not "losing" your instance variable. Your instance variable is a query which will yield different results before and after your rake task. Your modified test executes the query before the rake task and saves something derived from the result in a local variable (i.e. the id of the first record). You could achieve a similar effect by saving the event as follows:

@update_event = Event.find_by(next_occurrence: Date.today)

since find_by returns an ActiveRecord not an ActiveRelation.

3
  • Thanks for you answer, maybe I'm just a bit confused then as I thought the point at which I took a record from the db and assigned it to @update_event was before I ran my rake task?
    – Richlewis
    May 23, 2015 at 18:06
  • Nope. where returns a query object (i.e. an ActiveRelation) which doesn't execute until you call a method on it which performs the execution (such as first or all). May 23, 2015 at 18:08
  • Wow, did not know that, well that clears everything up, thank you
    – Richlewis
    May 23, 2015 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.