我正在用 symfony2 做一些 PHPUnit 测试。我在一项特定测试中遇到问题。
我正在测试我类(class)之一的回应,当然一个回应是对的,一个是错的。我有一个我的数据库的模拟,并且我有一个来 self 的 databaseRepository 的方法的 stub 。
问题是,在一个测试中,我对具有有效数组的方法进行了 stub ,在第二个测试中,我只想查询无效的 null。
我的数据库模拟:
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRecord')
->willReturn(null);
所以我试图有两个不同的期望,但是这显然是行不通的......是否有可能有一个 stub 方法有两个不同的返回..?
测试 1:
<?php
class UnsubscribeRegistrationTemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnsubscribeRegistrationTemplate
*/
protected $object;
/**
* @var ValidationClass
*/
public $validate;
/**
* @var DatabaseRepository
*/
public $db;
//Database Record Mock
public static $registrationRecord = array
(
'rowid' => '96',
'unsubscription' => 'N',
'updated' => 'BB'
);
/**
*
*/
protected function setUp()
{
//Setting up mock of validation class
$this->validate = $this->getMockBuilder('ValidationClass')
->disableOriginalConstructor()
->getMock();
$this->validate->expects($this->any())
->method('validateInput')
->willReturn(true);
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(null));
$this->db->expects($this->any())
->method('setPreRegistrationEnquiryUnsubscriptionEnabled')
->willReturn(true);
$this->object = $this->createUnsubscribeRegistrationTemplateInstance();
}
/**
* @return UnsubscribeRegistrationTemplate
*
*/
public function createUnsubscribeRegistrationTemplateInstance()
{
//initialize Unsubscribe Registration Template
return new UnsubscribeRegistrationTemplate
(
$this->validate,
$this->db
);
}
/**
* @param array $mapping
* @return Request
*/
public function createRequest(array $mapping)
{
$request = new Request();
foreach ( $mapping as $k =>$v)
{
$request->query->set($k, $v);
}
return $request;
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateValidResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
true,
'Unsubscription successful'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateEmptyResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
false,
'Registration Record Not Found.'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testIsAlreadyRegisteredValidResponse()
{
//Testing record is already unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'Y'
);
$this->assertTrue($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
public function testIsAlreadyRegisteredInValidResponse()
{
//Testing record not unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'N'
);
$this->assertFalse($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
protected function tearDown()
{
unset($this->object);
}
}
最佳答案
您可以通过多种方式做到这一点。
这里有两种方式可能适合您的需求。
/**
* @test
*/
public function ifTrue()
{
$this->db->expects($this->once())
->method('getRecord')
->will($this->returnValue(self::$registrationRecord));
$request = $this->createRequest(array(
'id' => '10',
'code' => 'BB'
));
$response = new Response(
true,
'successful'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
* @test
*/
public function ifFalse()
{
$this->db->expects($this->once())
->method('getRecord')
->willReturn(null);
$request = $this->createRequest(array(
'id' => '10',
'code' => 'BB'
));
$response = new Response(
false,
'Record Not Found.'
);
$this->assertEquals($response, $this->object->create($request));
}
如您所见,有很多重复项,所以让我们使用 dataprovider .
protected function getDataForTest()
{
return array(
array(self::$registrationRecord, true, 'successful'),
array(null, false, 'Record Not Found.')
);
}
/**
* @dataProvider getDataForTest
* @test
*/
public function ifTrue($getRecordValue, $bool, $message)
{
$this->db->expects($this->once())
->method('getRecord')
->will($this->returnValue($getRecordValue);
$request = $this->createRequest(array(
'id' => '10',
'code' => 'BB'
));
$response = new Response(
$bool,
$message
);
$this->assertEquals($response, $this->object->create($request));
}
使用@dataProvider,您可以使用任意数量的值来测试所有情况。
关于PhpUnit 测试 stub 方法多次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714665/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当