I want to test this function: Inside my unit test file, I have these functions: And finally, I have these errors: Expectation failed for method name is “get” when invoked zero or more times Parameter 0 for invocation IlluminateHttpRequest::get(‘address1′, null): mixed does not match expected value. Failed asserting that two strings are equal. Expected :’address2′ Actual :’address1’ Expectation failed for
Tag: phpunit
Laravel: Test not Using Mocked Method
I want getNumber to return 200 in the test case, but when I run the test and inspect the result with $response->dump() I can see that the mock is not overriding the method as it is showing array:1 [0 => 100]. What am I missing? Controller: Model: Test: I am using Laravel 8 and referencing this documentation: https://laravel.com/docs/8.x/mocking#mocking-objects Answer In
PHPUnit: Best practice for when to use Mock Object method invocation matcher
I have been reading the PHPUnit docs online and have been using the mock object matchers but I am not sure when you should or shouldn’t use them. For the example below, let’s say I have a class Foo I am writing a test for: Foo class My Test My question is should I be using $this->once() or not in
Falling testing Laravel assertModelExists
Making a test for database with Laravel (8.44.0) assetion assertModelExists(), fails on an error. The test class looks like this What do you think is missing? Answer The assertModelExists method was added September 2021: https://github.com/laravel/framework/pull/38766 Version 8.44.0 was released May 2021: https://github.com/laravel/framework/releases/tag/v8.44.0 So just update Laravel to the latest 8.x release and you should be good to go.
How to assert if a phpunit test writes a certain log entry / how to assert if emails are sent
I have a service class in my symfony5 project on php8, which is sending emails following certain rules. With my test want to check if the correct mails are sent. This task exists for several projects, so I really want to find a solution for this. The method, which collects the receiver of the mails is currently private and you
PhpUnit: how to fake an outdated file?
I have a method in a class, which checks if a file is older than a day. It achieves this by getting the last change date of the file and comparing it with “now”: I want to write a Unittest faking an outdated file. I tried to change the file date with touch: The output gives me the information that
How to ignore specific private static function with PHPUnit?
I’m new to PHPUnit and wondering is it possible to write a test for which ignore specific method. The code is like examine whether $data is Valid or not, and if it find irregular data, send message to slack with it. My question is, is it possible to run a test without sending alert message, like ignore sendAlert function? If
(PHPUnit) How do i check multiple chained method calls with different arguments?
I get the following message after executing the test: How do i make sure that register is called 4 times with arguments D,E,F,G? without changing the register method? Code: Answer Ok cool, i will answer myself. Just added ->willReturnSelf() and boom green.
Setting up PHPunit – PHP CLI is not executing the shell script
I’m in a multi developer program. The other developer installed PHPUnit via composer and successfully is running tests. I took the composer files, ran composer update, but when I try to run the tests, I am receiving this error: Output: This is the content of the phpunit shell script. The shebang first line #!/usr/bin/env sh is not being output, but
PHPUnit can’t generate code coverage when running tests in separate processes
I have a problem that I could not find the solution on Google. When I run my unit tests with PHPUnit and Xdebug, I would like to do a code coverage. However, when I do that, I get this error: I realized that this is because in some of my tests there is this at the top: When I remove