我不想在特征(或使用特征时使用其他方法)中执行构造函数。可能吗?
trait test{
public function __construct()
{
echo 'test';
}
}
class myClass{
use test;
public function __construct(){
echo 'myClass';
}
}
new myClass();
Run Code Online (Sandbox Code Playgroud) 我从一周开始学习 phpunit。我不知道如何只模拟测试类中的一种方法。(这只是示例,所以我没有写命名空间)。也许你可以帮助我
class SomeService
{
public function firstMethod()
{
return 'smth';
}
public function secondMethd()
{
return $this->firstMethod() . ' plus some text example';
}
}
Run Code Online (Sandbox Code Playgroud)
并测试:
class SomeServiceUnitTest extends TestCase
{
private $someService;
public function setUp()
{
parent::setUp();
$this->someService = new SomeService();
}
public function tearDown()
{
$this->someService = null;
parent::tearDown();
}
public function test_secondMethod()
{
$mock = Mockery::mock('App\Services\SomeService');
$mock->shouldReceive('firstMethod')->andReturn('rerg');
exit($this->walletService->secondMethd());
}
}
Run Code Online (Sandbox Code Playgroud) 我在想如何模拟运输。文件(来自温斯顿节点模块)。我正在使用 jest,所以__mocks__/winston.ts是自动加载的。我认为我不能嘲笑它,因为有new
// LoggerFactory.ts
import { transports, TransportInstance } from "winston";
...
const transportList: TransportInstance[] = [
new transports.File({
name: `${tag}-error`,
filename: `${dirname}${filename}.error.log`,
json: false,
level: "error",
zippedArchive: false,
maxFiles: 14,
maxsize: 100000000
}),
new transports.File({
name: `${tag}-info`,
filename: `${dirname}${filename}.log`,
json: false,
level: "info",
maxFiles: 10,
zippedArchive: false,
maxsize: 100000000
})
];
...
// __mocks__/winston.ts
const winston = {
????
};
export default winston;
Run Code Online (Sandbox Code Playgroud)
错误:类型错误:无法读取未定义的属性“文件”
我需要访问$request->important通过方法。我需要它来基于此值验证名称
class TestCustom implements Rule
{
public function passes($attribute, $value)
{
//
}
public function message()
{
return 'some txt';
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
use App\Rules\TestCustom;
$request->validate([
'name' => ['required', new TestCustom],
'important' => ['required', 'string'],
]);
Run Code Online (Sandbox Code Playgroud) data我的表中有 json 类型的列。该查询不起作用:
Advert::where('data->price', '>', 2000)->get();
Run Code Online (Sandbox Code Playgroud)
作为回应,我得到了价格低于 2000 的元素。QueryBuilder 在数据库上运行它:
SELECT * FROM "adverts" WHERE "data"->>'price' > '2000'
Run Code Online (Sandbox Code Playgroud)
我不知道出了什么问题。看起来价格正在转换为字符串,但在数据库中保存为整数。
{"price": 500}
Run Code Online (Sandbox Code Playgroud) 我需要通过重载来模拟CurrencyEnum,但这不是最终目的,因为我需要向该模拟中添加接口。这不起作用:
Mockery::mock('overload:'.CurrencyEnum::class);
Run Code Online (Sandbox Code Playgroud)
错误:(..) must be an instance of \BaseCurrency, instance of \CurrencyEnum given。我看着,但Mockery\Container::mock我不知道该怎么做。例如我要测试TestingClass::first()方法
class CurrencyEnum implements BaseCurrency
{
/* methods */
}
class TestingClass
{
public function first(string $currencySymbol)
{
$abc = 'some_string';
return $this->second($abc, new CurrencyEnum($currencySymbol));
}
private function second(string $abc, BaseCurrency $currency)
{
/* code */
}
}
Run Code Online (Sandbox Code Playgroud) php ×3
laravel ×2
mockery ×2
phpunit ×2
unit-testing ×2
eloquent ×1
javascript ×1
jestjs ×1
laravel-5 ×1
mocking ×1
postgresql ×1
traits ×1
typescript ×1
validation ×1
winston ×1