Calling Developers!
We are reenergizing our code contribution process! Learn More

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

Options
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 Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer
    Options

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

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    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
    Options

    Please give reference to implement same.

  • Alberto Reyer
    Alberto Reyer Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer
    Options

    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
    Options

    no error is still there

  • Alberto Reyer
    Alberto Reyer Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer
    Options

    Can you show the codeception.yml from this module?

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options
    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 Lead Spryker Solution Architect / Technical Director Posts: 690 🪐 - Explorer
    Options

    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
    Options

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

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    after adding this helper

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    these are resolved after adding following helper lines:

    - \SprykerTest\Client\Testify\Helper\ClientHelper
                    - \SprykerTest\Client\Testify\Helper\DependencyProviderHelper
    
  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    But this error still exists:

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    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
    Options

    Here config value is coming null

  • U03TXRYL7U7
    U03TXRYL7U7 Posts: 64 🧑🏻‍🚀 - Cadet
    Options

    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
    Options

    Can you please check where I went wrong