gpt4 book ai didi

ruby - 如何在 rspec 中模拟我的父类(super class)

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:32 24 4
gpt4 key购买 nike

我有以下代码:

class Blurk
def initialize
# horror
end

def perform
# yet more horror
end
end

class Grunf < Blurk
def perform
super
# here some code to test
end
end

我想在 Grunf#​​perform 上测试代码,但我不知道如何模拟 Blurk

最佳答案

一般来说你不应该这样做。更好的方法(恕我直言)是对您的类定义进行一些细微的更改

class Blurk
def initialize
# horror
end

def perform
# yet more horror
exec_perform
end

protected

def exec_perform
raise "exec_perform must be overriden"
end
end

class Grunf < Blurk
def exec_perform
# here some code to test
end
end

并分别测试 BlurkGrunf(在 Blurk 测试中,您可以创建 TestClass 定义,以便确认 exec_perform 按预期运行)。在 Grunf 的测试文件中,您只需测试 exec_perform 方法。

关于ruby - 如何在 rspec 中模拟我的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com