我的任务是为遗留代码编写单元测试。我当前的测试是针对一个将 Doctrine 2 实体管理器作为构造函数参数的对象。我需要能够为其提供测试数据,以便可靠地测试其方法。
因此,理想情况下,我希望能够创建一个模拟实体管理器,然后创建一个模拟实体并将其附加到模拟实体管理器,然后在我的测试中使用它。这可能吗?如果是,怎么办?
这个项目使用的是 Symfony 2.4+,所以我很确定这会在设置中增加一两个问题。
最佳答案
不确定这是否适用于您在 Symfony(完整堆栈?)设置中的特定需求,但在我们使用 Symfony 组件(和 Doctrine)而不是完整堆栈的先前项目中,这更多或更多减去以下设置:
<?php
namespace Tests\SomeNameSpace;
use Doctrine\ORM\EntityRepository;
use SomeNameSpace\Repositories\SomeRepository;
use SomeNameSpace\SubjectUnderTesting;
class SubjectUnderTestingTest extends \PHPUnit_Framework_TestCase
{
public function testSomething()
{
$queryExpectedValue = 'define expected return from query';
/**
* Infering that the the Subject Under Test is dealing with a single
* repository.
*
* @var Doctrine\ORM\EntityRepository
*/
$repository = $this
->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->setMethods(array('findOneByEmail'))
->getMock();
$repository
->expects($this->once())
->method('findOneByEmail')
->will($this->returnValue($queryExpectedValue));
/**
* Now mock the EntityManager that will return the aforementioned
* Repository. Extend to more repositories with a returnValueMap or
* with clauses in case you need to handle more than one repository.
*
* @var Doctrine\ORM\EntityManager
*/
$entityManager = $this
->getMockBuilder('Doctrine\ORM\EntityManager')
->setMethods(array('getRepository'))
->disableOriginalConstructor()
->getMock();
$entityManager
->expects($this->once())
->method('getRepository')
->with('SomeNameSpace\Repositories\SomeRepository')
->will($this->returnValue($repository));
/**
* Let's instantiate our Subject Under Test passing the mock as
* required.
*
* This code above is tailored to a scenario where the SUT method
* being tested will call something like this in the method to test:
*
* $queryResult = $entityManager
* ->getRepository('SomeNameSpace\Repositories\SomeRepository')
* ->findOneByEmail($someEmail);
*
* @var SubjectUnderTesting
*/
$sut = new SubjectUnderTesting($entityManager);
/**
* Assertions time if they apply.
*/
$expected = 'define what to expect';
$this->assertEquals(
$expected,
$sut->callSomeMethodToTestThatDependsOnSomeQueryByEmail()
);
}
}
关于php - 有没有办法创建 Doctrine 2 实体管理器的模拟并在 Symfony 2 PHPUnit 测试中为其提供模拟实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416028/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/