Please help! Delegate returns null via Dipendency Injection.

Posted by Raj Aththanayake on Stack Overflow See other posts from Stack Overflow or by Raj Aththanayake
Published on 2010-06-08T04:39:19Z Indexed on 2010/06/08 4:42 UTC
Read the original article Hit count: 174

Filed under:

Can someone please help?

I use Google code’s Moq framework for mocking within my Unit Tests and Unity for Dependency Injection.

In my Test class

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");
 }

In my Test class CustomerService class

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
}

}

When I debug the code I can see CustomerSearchService has a proxy object, but the customer returns as null. Any ideas? Or is there something missing here?


Note: ICustomerSearchService I have implemented below method.

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

© Stack Overflow or respective owner

Related posts about mock