请帮忙!委托通过依赖注入返回null

时间:2010-06-08 04:39:19

标签: mocking

有人可以帮忙吗?

我使用Google代码的Moq框架在我的单元测试和Unity依赖注入中进行模拟。

在我的测试课程中

private Mock<ICustomerSearchService> CustomerSearchServiceMock = null;
private CustomerService customerService = null;

private void SetupMainData()
{
    CustomerSearchServiceMock = new Mock<ICustomerSearchService>();
    customerService = new CustomerService ();

    // CustomerSearchService is a property in CustomerService and dependency is  configuered via Unity
    customerService.CustomerSearchService = CustomerSearchServiceMock.Object;

     Customer c = new Customer ()
     {
         ID = "AT"
     };

     CustomerSearchServiceMock.Setup(s => s.GetCustomer(EqualsCondition)).Returns(c); 
}


[TestMethod]
public void GetCustomerData_Test_Method()
{
    SetupMainData()
    var customer = customerService.GetCustomerData("AT");
}


 public static bool EqualsCondition(Customer customer)
 {
     return customer.ID.Equals("AT");
 }

CustomerService类

public class CustomerService : ICustomerService
{

    [Dependency]
    public ICustomerSearchService CustomerSearchService { get; set; }


    public IEnumerable<SomeObject> GetCustomerData(string custID)
    {

      I GET Null for customer ?????}
      var customer = CustomerSearchService.GetCustomer (c => c.ID.Equals(custID));

       //Do more things
    }
}

当我调试代码时,我可以看到CustomerSearchService有一个代理对象,但客户返回null。有任何想法吗?或者这里缺少什么?


注意:ICustomerSearchService我已经实现了以下方法。

     Customer GetCustomer(Func<Customer, bool> predicate);

1 个答案:

答案 0 :(得分:0)

我在Rhino.Mocks中也看​​到了这个问题(模拟,lambdas和存根/期望)。参见:

http://groups.google.com/group/rhinomocks/msg/318a35ae7536d90a