我刚刚开始将RedBean用于一个小的PHP网页项目。我正在为每个班级进行单元测试。我不确定如何在单元测试中模拟RedBean。
问题:如何模拟RedBean类以支持单元测试?
这是我所做的。到目前为止,它似乎运行良好。我能够对模型进行单元测试,并且已经通过静态RedBean函数调用隔离了:
这是一些示例测试代码:
$db = M::mock( 'BeanDatabase' );
$bean = M::mock( 'Bean' );
$factory = M::mock( 'EntityFactory' );
$term_bean = M::mock( 'Bean' );
$term = M::mock( 'Term' );
$db->shouldReceive( 'dispense' )
->once( )
->andReturn( $bean );
$db->shouldReceive( 'find_all' )
->once( )
->with( 'term', 'WHERE termcategory_id = 5' )
->andReturn( array( $term_bean ) );
$factory->shouldReceive( 'create_entities_from_beans' )
->once( )
->with( array( $term_bean ) )
->andReturn( $term );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句