正如其他地方所述(使用Selenium WebDriver截取屏幕截图),您可以使用Selenium Webdriver类PHPUnit_Extensions_Selenium2TestCase轻松地使用PHPUnit截取屏幕截图.
但是,我找不到设置生成的屏幕截图的屏幕大小的方法.默认情况下,它们的宽度似乎限制为1000像素.
BTW:上面提到的Stackoverflow线程已关闭.所以我不能在那里发表我的问题.
我正在尝试学习单元测试的基础知识,我正在使用phpunit和laravel
这是我的控制器
class FatController extends BaseController {
public $result;
public function __construct($result)
{
$this->result = $result;
}
public function getResult()
{
return $this->$result;
}
}
Run Code Online (Sandbox Code Playgroud)
和测试类
class FatControllerTest extends TestCase {
/**
* A basic functional test example.
* @return void
*/
public $test;
public function setUp()
{
parent::setUp();
$this->test = new FatController('100%');
}
public function testResult()
{
$result = $this->test->getResult();
$this->assertTrue($result == '100%', 'message');
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,当测试运行时,它应该创建一个新的变量$ test,它等于FatController的一个新实例,值为100%
然后它运行$ test FatController方法getResult,它应该返回$ result,它已被构造函数定义为'100%'
当测试运行时,我得到了这个
PHP Warning: Uncaught exception …Run Code Online (Sandbox Code Playgroud) 在phpspec中,如何测试类属性是否包含特定类型的数组?
例如:
class MyClass
{
private $_mySpecialTypes = array();
// Constructor ommitted which sets the mySpecialTypes value
public function getMySpecialTypes()
{
return $this->_mySpecialTypes;
}
}
Run Code Online (Sandbox Code Playgroud)
我的规格看起来像这样:
public function it_should_have_an_array_of_myspecialtypes()
{
$this->getMySpecialTypes()->shouldBeArray();
}
Run Code Online (Sandbox Code Playgroud)
但我想确保数组中的每个元素都是类型 MySpecialType
什么是在phpspec中做到这一点的最好方法?
还在学习如何测试php
我现在有一个工作界面(我认为) - 其中一个函数旨在创建一系列记录,我现在想要测试它.我承认我对测试知之甚少,所以问题多于知识.
所以
我的界面目前看起来像这样:
interface TicketCreatorInterface {
public function createTicket($input, $book);
}
Run Code Online (Sandbox Code Playgroud)
我的'repository'类看起来像这样:
Class TicketCreator implements TicketCreatorInterface {
protected $ticket;
public function __construct(TicketAudit $ticketAudit)
{
$this->ticket = $ticketAudit;
}
public function createTicket($input, $book) {
$counter = $input['start'];
while($counter <= $input['end']) {
$this->$ticket->create(array(
'ticketnumber'=>$counter,
'status'=>'unused',
'active'=>1
));
$this->ticket->book()->associate($book);
$counter = $counter+1;
}
return $counter;
}
Run Code Online (Sandbox Code Playgroud)
我在测试中的尝试看起来像这样:
public function testCreateCreatesTickets(TicketCreatorInterface $ticketCreator) {
//arrange
$book = Mockery::mock('Book');
//act
$response = $ticketCreator->createTicket(array('start'=>1000, 'end'=>1001), $book);
// Assert...
$this->assertEquals(true, $response);
}
Run Code Online (Sandbox Code Playgroud)
我首先尝试没有输入接口,因为我没有得到任何对象的错误.我尝试在界面上创建一个实例,但你不能这样做,所以在函数中使用了typehinting
我运行测试时得到的错误是:
Argument …Run Code Online (Sandbox Code Playgroud) 请解释为什么Laravel 4的示例测试失败了.
我的"/"路由重定向到我的routes.php文件中的"login".
(哦,我是RTFMed并搜索了几个网站,博客和内容.)
-sh-3.2$ phpunit
PHPUnit 4.0.7 by Sebastian Bergmann.
Configuration read from /home/dev/phpunit.xml
The Xdebug extension is not loaded. No code coverage will be generated.
F
Time: 83 ms, Memory: 13.50Mb
There was 1 failure:
1) ExampleTest::testBasicExample
Failed asserting that false is true.
/home/dev/app/tests/ExampleTest.php:14
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
-sh-3.2$
Run Code Online (Sandbox Code Playgroud)
这是ExampleTest.php中的代码
<?php
class ExampleTest extends TestCase {
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的服务.此服务调用其他服务security.context.当AlbumHandler调用时,模拟在该security.context步骤失败.
错误:
PHP Fatal error: Call to a member function getToken() on a non-object
Run Code Online (Sandbox Code Playgroud)
码:
public function setUp()
{
$this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface');
}
public function testAdd()
{
// The user I want to return
$user = $this->getMock('\MyProject\Bundle\UserBundle\Entity\User');
// I create a Token for mock getUser()
$token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');
$token->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
// I mock the service. PHPUnit don't return an error here.
$service = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')
->disableOriginalConstructor()
->getMock();
$service->expects($this->once())
->method('getToken')
->will($this->returnValue($token));
// I replace the …Run Code Online (Sandbox Code Playgroud) 我很难决定或理解在Laravel中测试的最佳方法.
我非常喜欢PHPSpec测试的行为方面,尽管它与测试Eloquent模型或与活动记录ORM相关的任何内容都不兼容.
当测试像服务提供商PHPSpec这样的东西似乎是要走的路.
**是否需要使用像PHPUnit这样的模型测试模型,然后测试其他非ORM层,例如PHPSpec之类的服务提供商?***
我在Laravel中创建了一个简单的User Repository类,它将处理所有CRUD功能.
一切正常,但是当我尝试使用phpunit进行单元测试时,它们都会分崩离析.
我正在运行Laravel的最新开发版本,我知道这不是一个稳定版本,但我很困惑这是一个laravel错误还是我做错了什么.
运行phpunit时,我得到了这个巨大的错误输出.
我的课程是
模范角色
namespace Shazzam\Models;
use Illuminate\Database\Eloquent\Model;
class Role extends Model {
protected $fillable = ['name'];
}
Run Code Online (Sandbox Code Playgroud)
角色存储库
namespace Shazzam\Repositories;
use \Shazzam\Models\Role;
class RoleRepository
{
/**
* @param array $args
* @return bool
*/
public function create($args)
{
$role = new Role($args);
return $role->save();
}
}
Run Code Online (Sandbox Code Playgroud)
RoleRepository测试
use Shazzam\Repositories\RoleRepository;
class RoleRepositoryTest extends TestCase
{
public $repo;
public function setUp()
{
$this->repo = new RoleRepository;
}
public function test_it_creates_a_new_role()
{
$role['name'] = "NewRole";
$this->assertTrue($this->repo->create($role)); …Run Code Online (Sandbox Code Playgroud) 我怎样才能让容器能够在测试类中获得mys服务.我找到了解决办法require_once dir(__FILE__). '/...../AppKernel.php;,但是当我WebTestCase上课并找到了
protected static function createClient(array $options = array(), array $server = array())
{
static::bootKernel($options);
$client = static::$kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
Run Code Online (Sandbox Code Playgroud)
哪里有内核启动,另外一个类KernelTestCase让我想知道我需要require_onceAppKernel文件还是存在一些更灵活,更好的方法来获取服务容器?谢谢!