问题标签 [mockery]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
3191 浏览

php - 测试具有模型作为依赖项的 laravel 存储库

问题是我无法测试一个功能,因为它正在触及同一存储库的其他功能。

  1. 我是否需要将一个功能与同一存储库中的其他功能分开测试,或者一个功能可以访问同一存储库中的其他功能是正常的?
  2. 如果函数需要与其他函数隔离测试,它是如何完成的,因为我不明白如何模拟我正在工作的存储库。我了解如何模拟依赖项,但如何模拟同一存储库中的其他功能?
  3. 我在测试中的 setUp 方法中正确地模拟模型吗?

代码:

真实世界的绑定和存储库:

EloquentUser.php:

EloquentUserTest.php

运行此测试时,我得到:

所以这意味着 Test 正在尝试触摸 EloquentUser.php 中的函数:

我需要模拟 createProfile 吗?因为 profile() 找不到。如果我需要这样做,我该怎么做,因为这个函数在我正在测试的同一个存储库中?

0 投票
1 回答
7199 浏览

unit-testing - 未找到“嘲弄”类

我使用 laravel (4.1) 框架并阅读了“Laravel-testing-decoded”,这是 Jeffrey Wey 的电子书。

我想测试我的模态用户和我的方法setPasswordAttribute($password)

我的单元测试:

但是当我启动 CLI 时:phpunit它返回一个错误:Fatal error: Class 'Mockery' not found

完全错误:

我不明白,为什么我有这个错误。

0 投票
1 回答
3703 浏览

unit-testing - laravel 4 嘲弄模拟模型关系

假设我有两个模型,Eloquent它们相互关联。我可以嘲笑这种关系吗?

IE:

在 MyTest 中,我当然想创建一个模拟,并通过调用 track 属性而不是 track 实例返回一个 track 实例我不想要查询生成器

0 投票
4 回答
8432 浏览

unit-testing - Laravel Response::download() test

I have the following code in one of my routes:

return Response::download('cv.pdf');

Any idea how to test this? I've tried to use shouldReceive() but that doesn't seem to work ('shouldReceive() undefined function....').

0 投票
1 回答
2735 浏览

unit-testing - Laravel Queue::shouldReceive()

I have the following in one of my routes

I am trying to write a unit test for the success scenario, I have the following:

But I keep receiving the following error when trying to run phpunit:

BadMethodCallException: Method Illuminate\Queue\QueueManager::connected() does not exist on this mock object

Any ideas?

0 投票
1 回答
1363 浏览

laravel - 如何为 Laravel Facade 指定 Partial Mockup?

首先我试过这个

但它给了我

所以后来我发现 Laravel Facades 已经实现了 Mockery,所以我直接在外观上进行了尝试。

但现在的问题是它没有找到该对象的其他函数,我需要在这里部分模型的行为,但我不知道如何告诉它。

这就是我需要的

0 投票
3 回答
3168 浏览

php - 带有回调参数的模拟方法

我正在尝试模拟此方法:

它接受回调参数作为第二个参数,我不知道如何模拟它。

我正在使用 Mockery,所以看起来像这样:

如果我将相同的回调传递给 with() 方法,则实例不匹配。如果 Mockery 不支持,我不介意使用 PHPUnit 模拟。

0 投票
1 回答
1685 浏览

laravel - Class 'Eloquent' not found when mocking in Laravel

I'm following through Jeffrey Way's Laravel Testing Decoded and I've hit an issue I can't seem to fix.

I'm actually work through this tutorial: http://net.tutsplus.com/tutorials/php/testing-laravel-controllers/ Which is an excerpt from his book.

Basically I have a test like so:

And that like for mocking Eloquent and Post returns:

When I run phpunit. Incidentally if I use Jeffrey's Laravel Generators and just generate some scaffold e.g.

And run phpunit I get same error. He's using the same:

To mock the classes. Does anyone have any suggestions on what the issue could be?


I've been working through the tutorial again from scratch and am still getting the same error. I've pushed it to a public repo so people can see: https://github.com/RyanHavoc/tdd-laravel

Just pull it down, run composer install/update and phpunit.

0 投票
1 回答
515 浏览

php - 嘲弄 __call() 有不同的签名

我正在使用 Mockery 用__call()魔术方法模拟一个类。

问题是我的模拟对象__call()与真实类的签名不同,我收到此错误:

以下是我的模拟程序:

我也尝试进行部分模拟,但仍然出现上述错误。

0 投票
1 回答
616 浏览

php - Laravel 4.1 PHPUnit 测试:在非对象上调用成员函数 with()

您好我正在尝试编写 PHPUnit 测试来测试我的登录控制器,并且我的代码似乎在浏览器中运行良好但是当我运行 php 单元测试时出现以下错误

错误:

方法:

单元测试:

在使用嘲笑方面我有点菜鸟,所以我想问题可能在于我如何嘲笑重定向对象?