Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock library in controllers tests? #304

Closed
dhilst opened this issue Jul 29, 2019 · 1 comment
Closed

How to mock library in controllers tests? #304

dhilst opened this issue Jul 29, 2019 · 1 comment

Comments

@dhilst
Copy link

dhilst commented Jul 29, 2019

How to mock libraries in controllers tests?

I saw that the reset_instance() is called during the $this->request(...) call, so if I do

public function setUp()
{
     $this->CI->email = new Mock_Libraries_Email();
}

public function testFoo()
{
    $this->request(...)
}

The mocked library is never called. I would prefer to replace mock library by some vanilla createMock instance, but this way would already help.

@dhilst
Copy link
Author

dhilst commented Jul 29, 2019

Okay, I find out, and in fact is covered in the documentation, sorry :)

The trick is to use setCallablePreConstructor() and load_class_instance to mock after the controller instantiation. Here is a sample

        $this->request->setCallablePreConstructor(function () use ($cmsEmailMock) {
            load_class_instance('cms_email', $cmsEmailMock);
        });
        $response = $this->request('POST', 'api/foo/',[])

@dhilst dhilst closed this as completed Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant