我正在学习python
我想知道是否有一种机制可以将一个对象(在我的情况下是一个假对象)"注入"到被测试的类中,而无需在costructor/setter中明确添加它.
## source file
class MyBusinessClass():
def __init__(self):
self.__engine = RepperEngine()
def doSomething(self):
## bla bla ...
success
## test file
## fake I'd like to inkject
class MyBusinessClassFake():
def __init__(self):
pass
def myPrint(self) :
print ("Hello from Mock !!!!")
class Test(unittest.TestCase):
## is there an automatic mechanism to inject MyBusinessClassFake
## into MyBusinessClass without costructor/setter?
def test_XXXXX_whenYYYYYY(self):
unit = MyBusinessClass()
unit.doSomething()
self.assertTrue(.....)
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我想"注入"对象"引擎",而不是在构造函数中传递它.我尝试了很少的选项(例如:@patch ......)没有成功.
我的机器上安装的默认 Perl 版本是 5.8.7。当我在内部运行脚本时切换到另一个 perl 版本 (v5.10.1) 执行以下操作:
my $perl_5_10 = "/opt/perl_5.10.1/bin";
$ENV{'PATH'} = $perl_5_10 ":" . $ENV{'PATH'};
Run Code Online (Sandbox Code Playgroud)
现在,我必须检查 perl 版本,我这样做:
## PERL: need perl version >= 5.10!
if ($] < 5.010000)
{
## VERSION ERROR!
}
my $perl_cmd = "perl --version";
my $perl_str=`$perl_cmd`;
print "PERL VERSION = " . $perl_str; ## this clearly print 5.10.1
Run Code Online (Sandbox Code Playgroud)
它返回错误,因为使用的版本是 5.8.7,这很正常,因为我使用该版本运行脚本。但我的问题是:
如何检查新的 perl 版本是否 >= 5.10.1 ?
我在一列中有一个 ngx-form-settings 和一个下拉对象。此下拉列表应由查询填充(通过服务调用)。
这是设置:
site: {
title: 'SITE',
type: 'html',
editor: {
type: 'list',
config: {
list: [],
},
}
},
Run Code Online (Sandbox Code Playgroud)
如果我这样做,它会起作用(当然这不是我需要的,因为 setList 必须位于 getData.subscribe 中):
ngOnInit() {
this.service.getData().subscribe((data: any) => {
});
this.setList();
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,它不起作用:
ngOnInit() {
this.service.getData().subscribe((data: any) => {
this.setList();
});
}
Run Code Online (Sandbox Code Playgroud)
其中设置列表就是这样(目前):
setList() {
this.settings.columns.site.editor.config.list = [{ value: 'Option 1', title: 'Option 1' },
{ value: 'Option 2', title: 'Option 2' },
{ value: 'Option 3', title: 'Option 3' },
{ value: 'Option 4', …Run Code Online (Sandbox Code Playgroud)