2010-06-08 52 views
0

返回null有人可以帮忙吗?请帮忙!委托通过Dipendency Injection

我使用Google代码的Moq框架在我的Unit Tests和Unity for Dependency Injection中进行模拟。

在我的测试类

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有一个代理对象,但客户的回报为空。有任何想法吗?还是有什么缺失?


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

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

回答