What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hi Please let me know how can I write unit test cases of the function having redis dependency. In fu

U03TXRYL7U7
U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

Hi
Please let me know how can I write unit test cases of the function having redis dependency.
In function I am getting the value from redis after passing key.

public function getGlobalConfigValueByKey(string $configKey)
    {
        $configValueData = $this->getFactory()->getStorageClient()->get(GlobalConfigConfig::REDIS_GLOBAL_CONFIG_KEY . ':' . strtolower($configKey));
        unset($configValueData['_timestamp']);

        return json_encode($configValueData);
    }

Comments

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Mock the factory and the storage client to return a JSON you define in your test.

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    I am trying to use
    $storageClient = $this->getStorageClientMock();

    But it is giving error
    I have tried to add following helper in codeception.yml

    • \SprykerTest\Client\Storage\Helper\StorageHelper

    But still error is there

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    Please give reference to implement same.

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Did you called docker/sdk cli vendor/bin/codecept ?
    Normally you don't get the methods of the test helpers in the parent class but on the tester, try $this->tester->getStorageClientMock()

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    no error is still there

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Can you show the codeception.yml from this module?

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet
    namespace: PyzTest\Zed\GlobalConfig
    paths:
        tests: .
        data: _data
        support: _support
        log: _output
    
    coverage:
        enabled: true
        remote: false
        whitelist: { include: ['../../../../src/*'] }
    
    suites:
        Business:
            path: Business
            class_name: GlobalConfigBusinessTester
            modules:
                enabled:
                    - Asserts
                    - \SprykerTest\Shared\Testify\Helper\Environment
                    - \SprykerTest\Shared\Testify\Helper\ConfigHelper
                    - \SprykerTest\Shared\Testify\Helper\DependencyHelper
                    - \SprykerTest\Shared\Propel\Helper\TransactionHelper
                    - \SprykerTest\Zed\Sales\Helper\BusinessHelper
                    - \SprykerTest\Shared\Testify\Helper\LocatorHelper:
                        projectNamespaces: ['Pyz']
    
  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Your codeception.yml for this module is missing the \SprykerTest\Client\Storage\Helper\StorageHelper afterwards build the tests again and it should be fine.

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    I have tried same but it is giving error in other test cases

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    after adding this helper

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    these are resolved after adding following helper lines:

    - \SprykerTest\Client\Testify\Helper\ClientHelper
                    - \SprykerTest\Client\Testify\Helper\DependencyProviderHelper
    
  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    But this error still exists:

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    I have tried the following:

    $arr = [
                "default" => "<https://selectandconfig-widget.schneider-electric.com/>",
                "zh_CN" => "<https://selectandconfig-widget.schneider-electric.cn/>",
                "_timestamp" => 1667978400.8121
            ];
            $storageClient = $this->getMockBuilder(StorageClient::class)
                ->onlyMethods(['get'])
                ->getMock();
            $storageClient
                ->method('get')
                ->willReturn($arr);
    
            $configStorageFactoryMock = $this->getMockBuilder(GlobalConfigPersistenceFactory::class)
                ->onlyMethods(['getStorageClient'])
                ->getMock();
    
            $configStorageFactoryMock
                ->method('getStorageClient')
                ->willReturn($storageClient);
    
            $configValue = $this->tester->createFacade()->getGlobalConfigValueByKey("scwidgetUrl");
            dd($configValue);
    
  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    Here config value is coming null

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    This is the function I am testing:

    public function getGlobalConfigValueByKey(string $configKey)
        {
            $configValueData = $this->getFactory()->getStorageClient()->get(GlobalConfigConfig::REDIS_GLOBAL_CONFIG_KEY . ':' . strtolower($configKey));
    
            unset($configValueData['_timestamp']);
    
            return json_encode($configValueData);
        }
    
  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 πŸ§‘πŸ»β€πŸš€ - Cadet

    Can you please check where I went wrong