The below code throws the following error:
Test method Informedica.GenForm.Library.Tests.UnityTests.IProductServicesTest.Save_product_calls_product_repository_to_save_the_product threw exception:
TypeMock.TypeMockException:
*** Isolate.Verify does not support objects that were not faked using Isolate.Fake.Instance(), or passed through WhenCalled()
If I ommit the [Isolated] attribute I get an index out of bounds error. When I use Isolate.Verify.WasCalledWithArguments, there is no error but the tests passes, should fail thought. So, somehow, Isolate does not spy on the right object.
[Isolated]
[TestMethod]
public void Save_product_calls_product_repository_to_save_the_product()
{
var product = LibraryObjectFactory.GetImplementationFor<IProduct>();
try
{
//GetProductServices().SaveProduct(product);
Isolate.Verify.WasCalledWithExactArguments(() => GetProductRepository(product).SaveProduct(product));
}
catch (Exception e)
{
if (e.GetType() != typeof(VerifyException)) throw;
Assert.Fail("product repository was not called to save product");
}
}
private IProductRepository GetProductRepository(IProduct product)
{
var repos = Isolate.Fake.Instance<ProductRepository>();
Isolate.WhenCalled(() => repos.SaveProduct(product)).IgnoreCall();
return repos;
}
However, when I change the code to inline the helper method GetProductRepository, the code runs as expected:
[Isolated]
[TestMethod]
public void Save_product_calls_product_repository_to_save_the_product()
{
var product = LibraryObjectFactory.GetImplementationFor<IProduct>();
try
{
//GetProductServices().SaveProduct(product);
var repos = Isolate.Fake.Instance<ProductRepository>();
Isolate.WhenCalled(() => repos.SaveProduct(product)).IgnoreCall();
Isolate.Verify.WasCalledWithExactArguments(() => repos.SaveProduct(product));
}
catch (Exception e)
{
if (e.GetType() != typeof(VerifyException)) throw;
Assert.Fail("product repository was not called to save product");
}
}
private IProductRepository GetProductRepository(IProduct product)
{
var repos = Isolate.Fake.Instance<ProductRepository>();
Isolate.WhenCalled(() => repos.SaveProduct(product)).IgnoreCall();
return repos;
}