我目前正在学习如何在 PHPUnit 框架中测试数据库,遇到了我不想在测试中连接到真实数据库的问题。这是因为当我在另一台计算机上运行测试时,这台计算机可能没有相同的数据库。
我实现了 \PHPUnit\DbUnit\TestCaseTrait 特性并设置了以下方法:
/**
* Returns the test database connection.
*
* @return \PHPUnit\DbUnit\Database\Connection
*/
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
return $this->createDefaultDBConnection($pdo, ':memory:');
}
/**
* Returns the test dataset.
*
* @return \PHPUnit\DbUnit\DataSet\IDataSet
*/
protected function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/test-dataset.xml');
}
数据集文件存在,并且已正确找到。
在我测试的 setUp 方法中,我将对象中的一个变量设置为 \PDO 实例。
/**
* @var PDO $databaseServerConnection
*/
private $databaseServerConnection;
public function setUp()
{
$this->databaseServerConnection = $this->getConnection()->getConnection();
}
我希望我现在可以在 getDataSet() 方法中将 PDO 连接与来自数据集文件的数据一起使用。
为了我自己的尝试,我尝试将它们与以下代码进行比较:
# Specify the tables we want to have in our connection dataset
$tables = ['users'];
# Create the dataset in the connection with the tables
$dataset = $this->getConnection()->createDataSet($tables);
# Query all results from the user table in the connection
$queryTable = $this->getConnection()->createQueryTable(
'users', 'SELECT * FROM users'
);
# Get the raw table data from the dataset file
$expectedTable = $this->getDataSet()->getTable('users');
# Check if theyre equal
$this->assertTablesEqual($queryTable, $expectedTable);
调试时,我注意到 $dataset 中的 $tables 数组变量只是一个空变量。这里是 $dataset 变量的 var_dump。
class PHPUnit\DbUnit\Database\FilteredDataSet#18 (3) {
protected $tableNames =>
array(1) {
[0] =>
string(5) "users"
}
protected $tables =>
array(0) {
}
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#16 (2) {
protected $connection =>
class PDO#15 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#17 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#15 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
}
此外,$queryTable 变量中的 $data 数组为 null。这里是 $queryTable 变量的 var_dump。
class PHPUnit\DbUnit\DataSet\QueryTable#22 (6) {
protected $query =>
string(19) "SELECT * FROM users"
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#20 (2) {
protected $connection =>
class PDO#19 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#21 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#19 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
protected $tableName =>
string(5) "users"
protected $tableMetaData =>
NULL
protected $data =>
NULL
private $other =>
NULL
}
此时 $expectedTable 变量中的 $data 数组充满了在数据集文件中创建的数据。
class PHPUnit\DbUnit\DataSet\DefaultTable#30 (3) {
protected $tableMetaData =>
class PHPUnit\DbUnit\DataSet\DefaultTableMetadata#34 (3) {
protected $columns =>
array(3) {
[0] =>
string(2) "id"
[1] =>
string(4) "name"
[2] =>
string(5) "email"
}
protected $primaryKeys =>
array(0) {
}
protected $tableName =>
string(5) "users"
}
protected $data =>
array(4) {
[0] =>
array(3) {
'id' =>
string(1) "1"
'name' =>
string(3) "test1"
'email' =>
string(9) "test1@me.nl"
}
[1] =>
array(3) {
'id' =>
string(1) "2"
'name' =>
string(3) "test2"
'email' =>
string(9) "test2@me.nl"
}
[2] =>
array(3) {
'id' =>
string(1) "3"
'name' =>
string(6) "test3"
'email' =>
string(12) "test3@me.nl"
}
[3] =>
array(3) {
'id' =>
string(1) "4"
'name' =>
string(4) "test4"
'email' =>
string(10) "test4@me.nl"
}
}
private $other =>
NULL
}
我还尝试在 getConnection() 方法内对 pdo 连接对象执行 2 个查询,以创建其中包含值的表:
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))");
$pdo->exec("INSERT INTO users (id, name, email) VALUES (20, 'Bas', 'aa@me')");
return $this->createDefaultDBConnection($pdo, ':memory:');
}
我的连接中没有任何可用数据是怎么来的,我怎样才能将数据集文件中的数据导入到这里以使测试通过?
此外,这样做是一种好的做法吗?
最佳答案
据我所见,setUp() 在你的 TestCase 中覆盖了 \PHPUnit\DbUnit\TestCaseTrait 的 setUp() 包含逻辑负责setting up and tearing down .
如果您需要在 TestCases 中使用不同的 setUp,那么将您自己的基类设为 described 可能是更好的主意在文档中并从中扩展您的 TestCase,然后从子 TestCase 调用 parent::setUp()
更新:
The Database, tables, sequences, triggers and views have to be created before you run the test suite.
取自here ,那里还有更多有用的提示。
基本上,这意味着在运行测试之前,所有表、列、索引、约束和其他与数据库相关的代码在测试中处理和依赖的东西都应该就位。运行前该数据库中的任何内容都无关紧要,TestCase 数据集中的所有表都将被截断并填充来自该数据集的数据。
更新 2:(免责声明:以下是我的个人偏好)
通常对数据库的访问是通过某种网关实现的。它们是我最后实现的(至少在包范围内)。当我开始做这些网关时,这让我有机会真正了解需要存储在数据库中的数据。因此,当开始在某个网关上编写一些 TestCase 时,我只是使用管理工具(通常是一些 GUI,如 phpMyAdmin)并创建表(表)以及我认为需要存在的列来存储网关处理的数据。然后编写测试并运行它们,可能会在更改表结构之间更好地适应。
通过这种方法,db 的所有结构都是手动创建的(而不是通过测试代码),并且它与在 db 上运行的代码一起增长。我发现它很方便有几个原因。
首先,这更简单,因为我不必在每个 TestCase 之前管理创建(或重新创建)结构。尤其是当我有几个使用相同表的测试用例时。
第二,我总是有适合测试通过的数据库结构。结构中任何不合适的变化都会被捕获。此外,我始终可以为这个适当的结构生成一个 sql 导出指令,以启动具有所有需要的表、列、索引、键等的现实生活数据库。
第三,有时需要(甚至必须)查看数据库,只考虑特定于数据库的问题。所以我总是可以打开当前的测试数据库并清楚地看到它是由什么组成的。
关于内存数据库的注释。 在这种情况下,需要在代码中创建结构。这里可能有两个明显的选择——为整个测试套件或特定测试用例(或其中的一组)设置具有特定结构的数据库。至于我,基于上述原因,我会做第一个。
最简单的实现方式是在 bootrstrap 文件中建立连接和创建结构。但我会投入一些时间并像这样添加一些动态:
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit>
<php>
<var name="MAKE_IN_MEMORY_DB" value="yes" />
</php>
</phpunit>
和:
abstract class AbstractDbTestCase extends \PHPUnit\DbUnit\TestCase
{
private $connection;
protected function getConnection() {
if($this->connection === NULL){
$this->connection = $this->createConnection();
}
return $this->connection;
}
private function createConnection(){
if($GLOBALS['MAKE_IN_MEMORY_DB'] == 'yes'){
return $this->createInMemory();
}
else {
return $this->createRealDbConnection();
}
}
private function createInMemory(){
// create connection and set up db;
}
private function createRealDbConnection(){
// create connection using some data from phpunit.xml
}
}
这将使测试与环境约束更加分离——运行测试只需要配置调整。实际上我会做更多,并使用带有 sql 语句的文件将它们加载到 createInMemory() 中(需要一些额外的工作,但我认为这是值得的)。
关于php - 在 PHPUnit 中的内存 SQLITE 数据库中模拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44647283/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>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
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
是的,我知道最好使用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
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢