ruby-on-rails – 在RSpec中使用细化
发布时间:2020-12-17 03:42:12 所属栏目:百科 来源:网络整理
导读:让我们说我有进步 module RefinedString refine String do def remove_latin_letters #code code code code end endend 我在课堂上使用它演讲: class Speech using RefinedString def initialize(text) @content = text.remove_latin_letters endend 我已经
让我们说我有进步
module RefinedString refine String do def remove_latin_letters #code code code code end end end 我在课堂上使用它演讲: class Speech using RefinedString def initialize(text) @content = text.remove_latin_letters end end 我已经为RSpec编写了精简测试,现在我正在测试Speech类 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:“?nńóyińg”没有实现:remove_latin_letter 我不认为嘲笑它是一个很好的解决方案(但我可能错了!在这里嘲笑解决方案?) 所以我试过了 let(:text){described_class :: String.new(“?nńóyińg”)} 我不想在我的RSpec中使用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |