小编flo*_*aun的帖子

Vitest 模拟模块仅在一项测试中起作用,并在其他测试中使用实际功能

以下是我的问题的抽象,因此没有太大意义:

假设我有一个简单的实用程序callMethodIf,它返回另一个导入方法 ( blackbox) 的返回值。

~~/utils/call-method-if.js:

import { blackbox } from '~~/utils/blackbox';

export const callMethodIf = (condition) => {
    return blackbox(condition);
};
Run Code Online (Sandbox Code Playgroud)

~~/utils/blackbox.js:

export const blackbox = (condition) => {
    return { called: condition };
};
Run Code Online (Sandbox Code Playgroud)

我将如何运行一个测试用例来调用 的实际实现blackbox(),以及另一个测试用例来模拟 的返回值blackbox()

我尝试这样做:

import { describe, expect, it } from 'vitest';

import { callMethodIf } from '~~/utils/call-method-if';

describe('Call method if', () => {
    it('returns "called: true" if condition is true', () => {
        const result = …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing nuxt.js vitest

11
推荐指数
2
解决办法
3万
查看次数

symfony4命令测试中的模拟服务

我的命令测试有问题。我正在尝试在命令测试中模拟服务,但存在一个问题,即该模拟未在测试中使用。

以下是命令代码:

public function __construct(RpcClient $rpcClient, LoggerInterface $logger, EntityManagerInterface $entityManager)
{
    $this->rpcClient = $rpcClient;
    $this->logger = $logger;
    $this->entityManager = $entityManager;

    parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $apiSecurityKey = $this->getContainer()->getParameter('api_security_key');
    try {
        $apiBoxesData = $this->rpcClient->callJsonRPCPostMethod("stations_info", ["apiSecurityKey" => $apiSecurityKey]); 
.
.
.
Run Code Online (Sandbox Code Playgroud)

并测试:

//some of dependencies used
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class SynchronizeBoxInfoCommandTest extends KernelTestCase
{

const SYNCHRONIZE_BOX_INFO_COMMAND_NAME = "app:synchronize-box-info";

public function setUp()
{
    parent::setUp();

    static::$kernel = static::createKernel();
    static::$kernel->boot();

    $application = new Application(static::$kernel);

    $this->command = $application->find(self::SYNCHRONIZE_BOX_INFO_COMMAND_NAME);
    $this->command->setApplication($application); …
Run Code Online (Sandbox Code Playgroud)

service unit-testing dependency-injection mocking symfony4

4
推荐指数
1
解决办法
1198
查看次数