The message ‘autodrive’ is sent to the car instance Ruby looks for the method ‘autodrive' and if it doesn't find the ‘autodrive’ method it calls ‘method_missing’ method_missing then throws the error NoMethodError
def setup FactoryBoy.define do factory :user do name "test" cat "meow" end end @user = FactoryBoy.build :user end def test_creates_user assert_equal @user.class, User end def test_has_name assert_equal @user.name, "test" end def test_meow assert_equal @user.cat, "meow" end end
def self.build(klass) instance = const_get(@factories[klass].class_name.capitalize).new @factories[klass].our_methods.each do |key, value| instance.class.class_eval do define_method(key.to_sym) { value } end end instance end def self.factory(klass, &block) instance = @factories[klass] = BaseObject.new(klass.to_s.capitalize) instance.instance_eval(&block) end class BaseObject < BasicObject attr_accessor :our_methods, :class_name def initialize(klass) @our_methods = {} @class_name = klass.to_s end def method_missing(method, *args, &block) @our_methods[method] = args.first end end end