diff --git a/README.md b/README.md index 4ef2a51..4096f70 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Mocks.create_struct_mock Example do end example = Example.new -allow(example).to receive(now).and_return(Time.new(2014, 12, 22)) +allow(example).to receive(now).and_return(Time.local(2014, 12, 22)) ``` ### Double diff --git a/spec/mocks/create_module_mock_spec.cr b/spec/mocks/create_module_mock_spec.cr index b1979a8..f225d84 100644 --- a/spec/mocks/create_module_mock_spec.cr +++ b/spec/mocks/create_module_mock_spec.cr @@ -10,8 +10,8 @@ Mocks.create_module_mock MyModule do mock self.exists?(name) end -Mocks.create_mock File do - mock self.exists?(name) +Mocks.create_module_mock Crystal::System::File do + mock self.exists?(path) end describe "create module mock macro" do @@ -22,7 +22,7 @@ describe "create module mock macro" do end it "does not fail with Nil errors for stdlib class" do - allow(File).to receive(self.exists?("hello")).and_return(true) + allow(Crystal::System::File).to receive(self.exists?("hello")).and_return(true) File.exists?("world").should eq(false) File.exists?("hello").should eq(true) end diff --git a/spec/mocks_spec.cr b/spec/mocks_spec.cr index 4297f29..2329676 100644 --- a/spec/mocks_spec.cr +++ b/spec/mocks_spec.cr @@ -9,6 +9,10 @@ class Example "#{greeting} world" end + def self.hello_world_default(greeting, object = "world") + "#{greeting} to the #{object}" + end + def say_hello "hey!" end @@ -42,13 +46,14 @@ end struct StructTimeExample def self.now - Time.new(2015, 1, 10) + Time.local(2015, 1, 10) end end Mocks.create_mock Example do mock self.hello_world mock self.hello_world(greeting) + mock self.hello_world_default(greeting, object = "world") mock instance.say_hello mock instance.say_hello(name) mock instance.greeting = value @@ -139,6 +144,13 @@ describe Mocks do Example.hello_world("halo").should eq("halo there world") end + it "works with class methods that have default values for arguments" do + Example.hello_world_default("hello").should eq("hello to the world") + + allow(Example).to receive(self.hello_world_default("halo", "earth")).and_return("halo there earth") + Example.hello_world_default("halo", "earth").should eq("halo there earth") + end + it "works with module methods" do ModuleExample.hello_world.should eq("what a wonderful world") @@ -147,10 +159,10 @@ describe Mocks do end it "works with struct methods" do - StructTimeExample.now.should eq(Time.new(2015, 1, 10)) + StructTimeExample.now.should eq(Time.local(2015, 1, 10)) - allow(StructTimeExample).to receive(self.now).and_return(Time.new(2014, 12, 22)) - StructTimeExample.now.should eq(Time.new(2014, 12, 22)) + allow(StructTimeExample).to receive(self.now).and_return(Time.local(2014, 12, 22)) + StructTimeExample.now.should eq(Time.local(2014, 12, 22)) end it "affects only the same class" do diff --git a/src/mocks/registry.cr b/src/mocks/registry.cr index e3ec157..3801cd1 100644 --- a/src/mocks/registry.cr +++ b/src/mocks/registry.cr @@ -83,7 +83,7 @@ module Mocks end def hash - object_id.hash * 32 + args.hash + object_id.hash &* 32 &+ args.hash end def inspect(io) @@ -122,7 +122,7 @@ module Mocks end def hash - @registry_name.hash * 32 * 32 + @name.hash * 32 + @object_id.hash + @registry_name.hash &* 32 &* 32 &+ @name.hash &* 32 &+ @object_id.hash end def inspect(io)