Hi Brian,
First - why the first one doesn't work (which is a bug :oops: ). Let's look at the IL that is created (thanks to Reflector):
[TestMethod]
public void UnitTestThatFails()
{
using (RecordExpectations recorder = new RecordExpectations())
{
CustomerManager.GetCustomer(new SearchObject());
Customer <>g__initLocal1 = new Customer();
<>g__initLocal1.LastName = "DOE";
recorder.Return(<>g__initLocal1);
}
}
As you can see, due to the code generation, the Return gets separated from GetCustomer, which creates the problem you see.
However, that's not all. If you verify or assert on the test that
passes, you'll fail also. This is because the initialization inside the recording block affects assignments to future objects that are initialized differently.
The best workaround for this is to get this line:
Customer expectedReturn = new Customer() {LastName = "DOE"};
using (RecordExpectations recorder = new RecordExpectations())
{
CustomerManager.GetCustomer(new SearchObject());
recorder.Return(expectedReturn);
}
outside from the recording block. The recording mechanism does not get confused, and everything works corretly.[/b]