PHPUNIT 如何测试在一种情况下不存在值的位置?
PHPUNIT how to test where a value doesn't exist in one case?
我正在学习 phpunit 测试实现,我的问题是我不知道如何测试 if 条件不成立的情况,所以它没有定义 $view->somethingSemantic 数组,那我怎么嘲笑
这个 PHPUNIT 测试能通过吗?
我应该在一次测试中测试这两种情况吗?或者我应该分成两部分...文档非常简单,但找不到任何类似案例的参考。
现在我收到一个错误 - #case 2 未定义 属性
测试 #2 始终失败,或者它显示 Love semantics 值与第一个数组 Love semantics 比较,就好像它们应该匹配一样通过测试?
// this is the function I'm creating the UT.
protected function _doSomeMagicPlease( $view ) {
if($this->config->DoSomeMagicPlease){
$view->somethingSemantic = array(
"Something else semantic" => $this->lsd(),
"Love semantics" => $this->magic()
);
}
}
// assertion - the view->somethingSemantic does not exist in case 2.
$this->assertEquals($expected , $view->somethingSemantic);
it seems it is comparing both arrays of the data provider:
// this is my data provider:
public function checkDoSomeMagicPleaseDataProvider()
{
return [
[
'message' => 'Case 1',
'configObject' => (object) array(
'values' => (object) array(
'DoSomeMagicPlease'=> 'true'
)
),
'expected' => array(
"Something else semantic" => "12345",
"Love semantics" => "https://www.someurl.com"
),
],
[
'message' => 'Case 2',
'configObject' => (object) array(
'values' => (object) array(
'DoSomeMagicPlease'=> "false"
)
),
'expected' => array(
"Something else semantic" => "12345",
"Love semantics" => "false"
)
]
];
}
我应该改变我的断言吗?任何提示表示赞赏!!
处理这个问题的一个简单方法是确保始终定义 somethingSemantic
。 null
或空数组似乎是合理的默认值。
public function checkDoSomeMagicPleaseDataProvider()
{
return [
// ...
[
'message' => 'Case 2',
'configObject' => (object)array(
'values' => (object)array(
'DoSomeMagicPlease' => 'false'
)
),
'expected' => null
]
];
}
如果您想保持原样,请将其分成两个单独的测试。您可以检查 somethingSemantic
属性是否存在于您的 view
对象上:
public function test_it_should_create_something_semantic_when_magic_is_enabled()
{
// ...
self::assertEquals(
[
'Something else semantic' => '12345',
'Love semantics' => 'https://www.someurl.com'
],
$view->somethingSemantic
);
}
public function test_it_should_not_create_something_semantic_when_magic_is_enabled()
{
// ...
self::assertObjectNotHasAttribute('somethingSemantic', $view);
}
我正在学习 phpunit 测试实现,我的问题是我不知道如何测试 if 条件不成立的情况,所以它没有定义 $view->somethingSemantic 数组,那我怎么嘲笑 这个 PHPUNIT 测试能通过吗?
我应该在一次测试中测试这两种情况吗?或者我应该分成两部分...文档非常简单,但找不到任何类似案例的参考。
现在我收到一个错误 - #case 2 未定义 属性 测试 #2 始终失败,或者它显示 Love semantics 值与第一个数组 Love semantics 比较,就好像它们应该匹配一样通过测试?
// this is the function I'm creating the UT.
protected function _doSomeMagicPlease( $view ) {
if($this->config->DoSomeMagicPlease){
$view->somethingSemantic = array(
"Something else semantic" => $this->lsd(),
"Love semantics" => $this->magic()
);
}
}
// assertion - the view->somethingSemantic does not exist in case 2.
$this->assertEquals($expected , $view->somethingSemantic);
it seems it is comparing both arrays of the data provider:
// this is my data provider:
public function checkDoSomeMagicPleaseDataProvider()
{
return [
[
'message' => 'Case 1',
'configObject' => (object) array(
'values' => (object) array(
'DoSomeMagicPlease'=> 'true'
)
),
'expected' => array(
"Something else semantic" => "12345",
"Love semantics" => "https://www.someurl.com"
),
],
[
'message' => 'Case 2',
'configObject' => (object) array(
'values' => (object) array(
'DoSomeMagicPlease'=> "false"
)
),
'expected' => array(
"Something else semantic" => "12345",
"Love semantics" => "false"
)
]
];
}
我应该改变我的断言吗?任何提示表示赞赏!!
处理这个问题的一个简单方法是确保始终定义 somethingSemantic
。 null
或空数组似乎是合理的默认值。
public function checkDoSomeMagicPleaseDataProvider()
{
return [
// ...
[
'message' => 'Case 2',
'configObject' => (object)array(
'values' => (object)array(
'DoSomeMagicPlease' => 'false'
)
),
'expected' => null
]
];
}
如果您想保持原样,请将其分成两个单独的测试。您可以检查 somethingSemantic
属性是否存在于您的 view
对象上:
public function test_it_should_create_something_semantic_when_magic_is_enabled()
{
// ...
self::assertEquals(
[
'Something else semantic' => '12345',
'Love semantics' => 'https://www.someurl.com'
],
$view->somethingSemantic
);
}
public function test_it_should_not_create_something_semantic_when_magic_is_enabled()
{
// ...
self::assertObjectNotHasAttribute('somethingSemantic', $view);
}