H Wessam,
There are few things that went wrong here.
The first one is caused by the following line :
r.Return(fakeCustomers.AsQueryable());
You must remember that ALL calls inside a recording blocks are mocked including in this case the call to AsQueryable(). To fix you will need to take that statement outside the recording block (I’ve put it on the creation of the fakeCustomers instance)
The next that goes wrong, is caused by the compiler in conjunction with the recording mechanism.
What we see as a simple LINQ query is actually "translated" (during compilation) by the compiler into a very complex piece of code.
In order for the Isolator framework to properly function the LINQ statement during the recording phase must be IDENTICAL to the one which is actually executed.
In our case, there is a small difference on this line:
where c.CustomerID == "1"
It appears that using a string literal and using a string variable is treated differently by the compiler, which in turn confuses the Isolator framework.
To bypass this you just need to define a string variable before the recording block set its value to "1" and then use it during the recording. (On our side we still need to think how to deal with this)
That last thing I have encountered, relates to the current implementation of CustomerController class that you have previously sent me. (I thought on mentioning it here cause it kind of confused me and I wanted to save you that pain). In that implementation, an instance of NorthwindDataContext object is created during the construction of the CustomerController Class . This creation conflicts with the creation you do in the GetCustomerName we have in this example.
What left me at the end with this test (I also remarked the entire BaseController constructor)
[TestMethod(),VerifyMocks]
public void TestingTest()
{
var fakeCustomer = new Customer() { CustomerID = "123", ContactName = "Me" };
var fakeCustomers = (new List<Customer>() { fakeCustomer });
string Lior = "1";
using (RecordExpectations r=new RecordExpectations())
{
NorthwindDataContext context = new NorthwindDataContext();
var customer = from c in context.Customers
where c.CustomerID == Lior
select c;
r.Return(fakeCustomers.AsQueryable());
}
CustomerController target = new CustomerController();
string customerID = "123";
string expected = "Me";
string actual;
actual = target.GetCustomerName(customerID);
Assert.AreEqual(expected, actual);
}
which passes on my side.
Last thing, I just want to verify that last week you received an email from regarding a similar LINQ issue you reported. if you didn’t please let me know.