Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/mocks/create_module_mock_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 16 additions & 4 deletions spec/mocks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/mocks/registry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down