mockery

Mocking Laravel Eloquent models - how to set a public property with Mockery

别来无恙 提交于 2019-12-20 17:41:17
问题 I want to use a mock object (Mockery) in my PHPUnit test. The mock object needs to have both some public methods and some public properties set. The class is a Laravel Eloquent model. I tried this: $mock = Mockery::mock('User'); $mock->shouldReceive('hasRole')->once()->andReturn(true); //works fine $mock->roles = 2; //how to do this? currently returns an error $this->assertTrue(someTest($mock)); ... but setting the public property returns this error: BadMethodCallException: Method Mockery_0

Mockery specifying expected arguments for multiple calls

淺唱寂寞╮ 提交于 2019-12-18 19:40:26
问题 I am trying to mock an object that gets two calls to the same function but with different arguments. It's pretty straight forward to give back different return values for multiple calls but I can't find anywhere how to do it with the argument validation. I tried: $this->eventDispatcher ->shouldReceive('dispatch') ->twice() ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event')) ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event'); And $this->eventDispatcher -

Mockery shouldReceive()->once() doesn't seem to work

一世执手 提交于 2019-12-18 19:05:16
问题 I'm trying to get Mockery to assert that a given method is called at least once. My test class is: use \Mockery as m; class MyTest extends \PHPUnit_Framework_TestCase { public function testSetUriIsCalled() { $uri = 'http://localhost'; $httpClient = m::mock('Zend\Http\Client'); $httpClient->shouldReceive('setUri')->with($uri)->atLeast()->once(); } } As you can see, there's one test that (hopefully) creates an expectation that setUri will be called. Since there isn't any other code involved, I

Testing chained method call in Mockery

爷,独闯天下 提交于 2019-12-18 03:38:13
问题 I'm trying to properly mock a chained call to an Eloquent model in a controller. In my controller I'm using dependancy injection to access the model so that it should be easy to mock, however I'm not sure how to test the chained calls and make it work right. This is all in Laravel 4.1 using PHPUnit and Mockery. Controller: <?php class TextbooksController extends BaseController { protected $textbook; public function __construct(Textbook $textbook) { $this->textbook = $textbook; } public

Mocking Static Eloquent Models Methods including find()

最后都变了- 提交于 2019-12-14 00:32:28
问题 I've been following the general Mockery and PHP-Unit tutorials - including Jeffrey Way's introduction to testing Laravel with PHP-Unit and Mockery. However, for this app - we're fine with a dependency on Eloquent, and would rather not create a repository class. We're able to mock instance methods of our Widget model fine. However, we're using Route:model binding and I confess I'm not exactly sure how to mock the find() method of the model when testing the show($widget) method of the

How to throw an exception on the Nth call of a mock method with Mockery

自闭症网瘾萝莉.ら 提交于 2019-12-13 17:05:47
问题 I need to test how some code I wrote will behave when it calls a method on another class multiple times, where one of the calls will cause an exception to be thrown. I am using Mockery to mock the class that may throw an exception. So in my case, the method will be called three times and I need it throw an exception on the second time. This is example of my intention but it doesn't work. $mock = \Mockery::mock(); $mock->shouldReceive('fetch') ->andReturnUsing( function () {return true;},

How to test routes in Laravel 5, or Trying to “MockStub” something, or I have no idea of TDD

我的梦境 提交于 2019-12-12 15:42:29
问题 I'm starting with TDD and Laravel. Specifically, I'm starting with routes. I defined some and I defined it badly, so excited as I was with the "new" concept of TDD I wanted to write some test for them. The idea was to test the routes and only the routes, in isolation, as everything I've readed about TDD recomends. I know I can do a $this->call->('METHOD','something') and test response is OK or whatever, but I would like to know that the right method of the right controller is called. So, I

Laravel 5 - Unit testing - status code 500, expected 200

眉间皱痕 提交于 2019-12-12 13:32:38
问题 Why unit test in "test 1" returns me status code 500, not 200 ? Can somebody explain me ? Here is example in 2 tests for same action and they return different status code. I expected 200 in both tests ? LanguageController class LanguageController extends Controller implements IEntityViewManager { public function showAllView() { $allLanguages = $this->languageRepo->orderBy('id'); return view('admin.languages.showAll')->with('languages', $allLanguages); } } LanguageControllerTest class

PHPUnit mockery and typehinted methods

匆匆过客 提交于 2019-12-11 23:53:17
问题 does anyone already tried phpunit with php7 methods type hints? I got an issue mocking class with typoe hinted method's like PHP Fatal error: Declaration of Mockery_0_Forms_Fields_TextField::getSettings() must be compatible with AbstractField::getSettings(): array in /home/n1ks2n/vagrant/$project-name/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(16) : eval()'d code on line 25 Beacuse i have methods like public function getSettings() : string {/**code here*/} in my code. Think

how to unit-test a php method_exists()

牧云@^-^@ 提交于 2019-12-11 13:35:20
问题 having this code <?php public function trueOrFalse($handler) { if (method_exists($handler, 'isTrueOrFalse')) { $result= $handler::isTrueOrFalse; return $result; } else { return FALSE; } } how would you unit-test it? is there a chance to mock a $handler ? obviously i would need some kind of <?php $handlerMock= \Mockery::mock(MyClass::class); $handlerMock->shouldReceive('method_exists')->andReturn(TRUE); but it cannot be done 回答1: Okay In your testCase class you need to use the same namespace