测试 class,它使用 RSpec 的改进
Testing class which uses refinements with RSpec
假设我有细化
module RefinedString
refine String do
def remove_latin_letters
#code code code code
end
end
end
我在 class 演讲中使用它:
class Speech
using RefinedString
def initialize(text)
@content = text.remove_latin_letters
end
end
我已经在 RSpec 中编写了用于改进的测试,现在我正在测试 Speech class
describe Speech
let(:text) { "ąńńóyińg" }
it 'should call my refinement' do
expect(text).to receive(:remove_latin_letters)
Speech.new(text)
end
end
但我得到 RSpec::Mocks::MockExpectationError: "ąńńóyińg" does not implement: remove_latin_letter
我认为 mocking 不是一个好的解决方案(但我可能错了!这里是 mocking 解决方案吗?)
所以我尝试了
let(:text) { described_class::String.new("ąńńóyińg") }
但是结果是一样的。
我不想在我的 RSpec 中显式调用 using RefinedString
(它应该自己解决,对吧?)
如何让 RSpec 了解我精炼的方法?
我们总是想测试行为,而不是实施。在我看来,改进通过被包含而不是拥有自己的行为来改变其他 类 的行为。用一个有点笨拙的类比,如果我们要测试病毒的繁殖行为,我们就必须将它引入宿主细胞。我们感兴趣的是当病毒接管时宿主会发生什么(可以这么说)。
一种方法是使用和不使用优化来构建测试 类,例如:
class TestClass
attr_reader :content
def initialize(text)
@content = text.remove_latin_letters
end
end
describe "when not using RefinedString" do
it "raises an exception" do
expect { TestClass.new("ąńńóyińg") }.to raise_error(NoMethodError)
end
end
class RefinedTestClass
using RefinedString
attr_reader :content
def initialize(text)
@content = text.remove_latin_letters
end
end
describe "when using RefinedString" do
it "removes latin letters" do
expect(RefinedTestClass.new("ąńńóyińg").content).to eq "ńńóń"
end
end
假设我有细化
module RefinedString
refine String do
def remove_latin_letters
#code code code code
end
end
end
我在 class 演讲中使用它:
class Speech
using RefinedString
def initialize(text)
@content = text.remove_latin_letters
end
end
我已经在 RSpec 中编写了用于改进的测试,现在我正在测试 Speech class
describe Speech
let(:text) { "ąńńóyińg" }
it 'should call my refinement' do
expect(text).to receive(:remove_latin_letters)
Speech.new(text)
end
end
但我得到 RSpec::Mocks::MockExpectationError: "ąńńóyińg" does not implement: remove_latin_letter
我认为 mocking 不是一个好的解决方案(但我可能错了!这里是 mocking 解决方案吗?)
所以我尝试了
let(:text) { described_class::String.new("ąńńóyińg") }
但是结果是一样的。
我不想在我的 RSpec 中显式调用 using RefinedString
(它应该自己解决,对吧?)
如何让 RSpec 了解我精炼的方法?
我们总是想测试行为,而不是实施。在我看来,改进通过被包含而不是拥有自己的行为来改变其他 类 的行为。用一个有点笨拙的类比,如果我们要测试病毒的繁殖行为,我们就必须将它引入宿主细胞。我们感兴趣的是当病毒接管时宿主会发生什么(可以这么说)。
一种方法是使用和不使用优化来构建测试 类,例如:
class TestClass
attr_reader :content
def initialize(text)
@content = text.remove_latin_letters
end
end
describe "when not using RefinedString" do
it "raises an exception" do
expect { TestClass.new("ąńńóyińg") }.to raise_error(NoMethodError)
end
end
class RefinedTestClass
using RefinedString
attr_reader :content
def initialize(text)
@content = text.remove_latin_letters
end
end
describe "when using RefinedString" do
it "removes latin letters" do
expect(RefinedTestClass.new("ąńńóyińg").content).to eq "ńńóń"
end
end