12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rspecでrake taskをテストする方法 

Posted at

ほとんど定型文です。理解しないでもかけます。

lib/tasks/sample.rake

task sample: :environment do
  user = User.first
  user.destroy!
end

spec/lib/tasks/sample_spec.rb


require 'rails_helper'
require 'rake'

describe 'sample' do
  before(:all) do
    @rake = Rake::Application.new
    Rake.application = @rake
    Rake.application.rake_require('sample', ["#{Rails.root}/lib/tasks"])
    Rake::Task.define_task(:environment)
  end


  describe 'sample' do
    subject { @rake['sample'].execute }
    
    it 'returns hoge' do
      user = create(:user)
      expect{ subject }.to change(User, :count).by(-1)
    end
  end
end


12
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?