I'm trying to mock a single method call in a controller I'm testing (this isn't a Rails question). I only want to mock the method for certain tests while the "real" method continues to function as before in the remainder of the tests. I'm trying to figure out how to use FlexMock to do this but I'm coming up empty.
Here's some example code showing what I'm trying to do, and what I'd like to mock.
require 'rubygems'
require 'uuid'
class StorageController
def get_next_uuid
UUID.new
end
def assign_uuid
3.times do |attempt|
uuid = get_next_uuid
return [uuid] if not @records.include?(uuid)
end
["Too many uuid collisions"]
end
end
And now for the tests...
class StorageControllerApiTest < Test::Unit::TestCase
include FlexMock::TestCase
def test_assign_uuid
# this call should use the real #get_next_uuid or real #UUID.new methods
result = @controller.assign_uuid
assert_equal 0, result[0] =~ /^([a-z\d]+)\-([a-z\d]+)\-([a-z\d]+)\-([a-z\d]+)\-([a-z\d]+)$/
# want to mock #get_next_uuid or #UUID.new here! Want to mock it out so I can force
# the #assign_uuid method to "fail" and returns the error message
result = @controller.assign_uuid
assert_equal 0, result[0] =~ /^Too many uuid collisions$/
end
end
So how do I mock out either #get_next_uuid or UUID.new? I thought mocking UUID.new would be easiest but I can't see how to use FlexMock to handle the case where there are no instance methods and only class methos (that is, you don't call UUID.new to get an instance of the UUID class, it just returns a value directly).
I asked a similar question on the Rails list but it occurred to me this is primarily a question about mocks. I'd like to get better at creating and using them within my tests.
Thanks for the help!
cr