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
17 changes: 13 additions & 4 deletions spec/mocks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end

struct StructTimeExample
def self.now
Time.new(2015, 1, 10)
Time.local(2015, 1, 10)
end
end

Expand All @@ -59,6 +59,10 @@ Mocks.create_mock AnotherExample do
mock self.hello_world
end

Mocks.create_mock File do
mock self.read_lines(filename, encoding = nil, invalid = nil, chomp = true)
end

Mocks.create_module_mock ModuleExample do
mock self.hello_world
end
Expand Down Expand Up @@ -139,6 +143,11 @@ describe Mocks do
Example.hello_world("halo").should eq("halo there world")
end

it "mocks File.read_lines" do
allow(File).to receive(self.read_lines("example")).and_return(["hey, world!\n"])
File.read_lines("example").should eq(["hey, world!\n"])
end

it "works with module methods" do
ModuleExample.hello_world.should eq("what a wonderful world")

Expand All @@ -147,10 +156,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