If I was using RSpec I could test if a method is being called like so:
expect(obj).to receive(:method)
What is the equivalent in MiniTest? I have a model, Post
, which has a before_validation
callback which runs a method create_slug
. In my test test/models/post_test.rb
I want to ensure that the create_slug
method is being called when calling post.save
.
The Minitest::Spec documentation says that I can use a method must_send
to check if a method is called. However, when I try @post.must_send :create_slug
I receive the following error:
NoMethodError: undefined method `must_send' for #<Post:0x007fe73c39c648>
I am including Minitest::Spec in my test_helper.rb
file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/spec'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
An excerpt of my test:
describe Post do
before do
@post = FactoryGirl.build(:post)
end
describe "when saving" do
it "calls the create_slug method before validation" do
@post.must_send :create_slug
@post.save
end
end
end
See Question&Answers more detail:os