我正在使用本教程伪造我的DbContext并测试:http://refactorthis.wordpress.com/2011/05/31/mock-faking-dbcontext-in-entity-framework-4-1-with-a-generic -repository /
但我必须更改FakeMainModuleContext实现以在我的控制器中使用:
public class FakeQuestiona2011Context : IQuestiona2011Context
{
private IDbSet<Credencial> _credencial;
private IDbSet<Perfil> _perfil;
private IDbSet<Apurador> _apurador;
private IDbSet<Entrevistado> _entrevistado;
private IDbSet<Setor> _setor;
private IDbSet<Secretaria> _secretaria;
private IDbSet<Pesquisa> _pesquisa;
private IDbSet<Pergunta> _pergunta;
private IDbSet<Resposta> _resposta;
public IDbSet<Credencial> Credencial { get { return _credencial ?? (_credencial = new FakeDbSet<Credencial>()); } set { } }
public IDbSet<Perfil> Perfil { get { return _perfil ?? (_perfil = new FakeDbSet<Perfil>()); } set { } }
public IDbSet<Apurador> Apurador { get …Run Code Online (Sandbox Code Playgroud) 我是针对ADO .NET实体框架编写的单元测试代码.我想用行填充内存数据库,并确保我的代码正确检索它们.
我可以使用Rhino Mocks来模拟实体框架,但这还不够.我会告诉查询返回给我的实体.这既不会测试where子句,也不会测试.Include()语句.我想确保我的where子句只匹配我想要的行,而不是其他行.我想确定我已经要求我需要的实体,而不是我没有.
例如:
class CustomerService
{
ObjectQuery<Customer> _customerSource;
public CustomerService(ObjectQuery<Customer> customerSource)
{
_customerSource = customerSource;
}
public Customer GetCustomerById(int customerId)
{
var customers = from c in _customerSource.Include("Order")
where c.CustomerID == customerId
select c;
return customers.FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我模拟ObjectQuery以返回一个填充了订单的已知客户,我怎么知道CustomerService有正确的where子句和Include?我宁愿插入一些客户行和一些订单行,然后断言选择了正确的客户并填充了订单.