mardi 31 mai 2016

How to use Minitest mock

Trying to test class method using Minitest::Mock

Included minitest in my Gemfile in development and test environments and bundled:

group :development, :test do
  gem 'byebug'
  gem 'minitest-rails'
end

In my Store class app/models/store.rb:

class Store < ActiveRecord::Base
  validates_presence_of :store_name,
                        :address, 
                        :number_of_employees

  def self.import(file)
    CSV.foreach(file.path, :headers => true,
                       :header_converters => lambda { |h|     h.downcase.gsub(' ', '_')},
                       :col_sep => "\t"
                       ) do |row|
       Store.create! row.to_hash
     end
   end
end

In my model test test/models/store_test.rb:

require 'minitest/autorun'
require File.expand_path("../../test_helper", __FILE__)


require 'minitest/mock'
require 'minitest/unit'
require 'date'

Minitest.autorun
class StoreTest < Minitest::Test

  def setup
    @mock = Store.new
    @mock.store_name = "Example Store"
    @mock.address = "111 Maple St"
    @mock.number_of_employees = 5
  end

  def should_import_csv_file_with_all_fields_present
    @mock = Minitest::Mock.new
    @mock.save!
    assert_equal "Example Store", @mock.store_name
    assert @mock.verify
  end
end

running test with ruby test/models/store_test.rb returns:

0 runs, 0 assertions, 0 failures, 0 errors, 0 skips

This is a basic question but it looks as though no tests are being run. Any help is much appreciated!

Aucun commentaire:

Enregistrer un commentaire