Created
May 20, 2023 14:30
Crystal Spectator - using `inject_mock`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec.cr | |
require "spectator" | |
require "./spec_helper" | |
Spectator.describe OrderCalculator do | |
inject_mock Database | |
def_mock(Product) | |
it "multiplies the product price by given quantity" do | |
product = new_mock(Product) | |
allow(Database).to receive(:product).and_return product | |
allow(product).to receive(:price).and_return 5.0 | |
expect(subject.total_price(quantity: 2)).to eq 10 | |
end | |
end | |
# code.cr | |
class OrderCalculator | |
def total_price(quantity : Int) : Float | |
Database.product.price * quantity | |
end | |
end | |
class Product | |
def price : Float | |
10.0 | |
end | |
end | |
class Database | |
def self.product : Product | |
Product.new | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec.cr | |
require "spectator" | |
require "./spec_helper" | |
Spectator.describe OrderCalculator do | |
inject_mock Database | |
it "multiplies the product price by given quantity" do | |
allow(Database).to receive(:product_price).and_return 5.0 | |
expect(subject.total_price(quantity: 2)).to eq 10 | |
end | |
end | |
# code.cr | |
class OrderCalculator | |
def total_price(quantity : Int) : Float | |
Database.product_price * quantity | |
end | |
end | |
class Database | |
def self.product_price : Float | |
0.0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment