chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
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;
        }

asked by halcwb (5.5k points)

2 Answers

0 votes
Hi,

The error message was actually accurate :)

Try writing:
var productRepository = GetProductRepository(product);

// Do something with productRepository...

Isolate.Verify.WasCalledWithExactArguments(() => productRepository.SaveProduct(product));


That way, you are arranging the object you are testing, acting on it, and only then asserting that it was called properly, and it is also more readable in my opinion...

Regards,
Yonatan
TypeMock Support Group
answered by yonatan (1.6k points)
0 votes
Oops, you are perfectly right. Just did not see it, next time first right the code inline and then refactor with extract method.

Thanks for the quick reply to a rather silly mistake.
answered by halcwb (5.5k points)
...