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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
hi the following throws an exception

Isolate.Fake.StaticMethods<HttpContext>(Members.ReturnRecursiveFakes);

            Assert.IsNotNull(HttpContext.Current.Request.Url.AbsoluteUri);

Exploring the tests.
Running the tests.
TestCase 'MbUnit v3.0.5.546/WebPeriodicService.Fixtures/WebPeriodicServiceTestFixture/Test_That_A_New_Unique_Item_Will_Be_Added_To_The_Cache'
failed: 
   System.ArgumentNullException: Value cannot be null.
   Parameter name: obj
   at System.CultureAwareComparer.GetHashCode(String obj)
   at System.Uri.CalculateCaseInsensitiveHashCode(String text)
   at System.Uri.GetHashCode()
   at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at ei.a(Object A_0, Object[] A_1, Object A_2)
   at ap.a(MethodBase A_0, Object A_1, Object[] A_2, Object A_3)
   at TypeMock.MockManager.a(Object A_0, MethodBase A_1, Object[] A_2, Mock A_3, Object A_4)
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
   at System.Uri.get_AbsoluteUri()
   E:\NET Solutions\Csharp\WM\WebPeriodicService\WebPeriodicService.Fixtures\WebPeriodicServiceTestFixture.cs(22,0): at WebPeriodicService.Fixtures.WebPeriodicServiceTestFixture.Test_That_A_New_Unique_Item_Will_Be_Added_To_The_Cache()
   at TypeMock.VerifyMocksAttribute.Execute()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.ClearMocksAttribute.Execute()
   at TypeMock.MethodDecorator.a()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
   E:\NET Solutions\Csharp\WM\WebPeriodicService\WebPeriodicService.Fixtures\WebPeriodicServiceTestFixture.cs(18,0): at WebPeriodicService.Fixtures.WebPeriodicServiceTestFixture.Test_That_A_New_Unique_Item_Will_Be_Added_To_The_Cache()

Unloading the test package.
asked by tolisss (28.8k points)

3 Answers

0 votes
Hi Tolisss,

I ran the code below and got a different result - the Assert.IsNotNull statement failed.

That result makes sense to me - recursive fakes are defined to return a fake object for any call returning a reference type, and the default value of any call returning a value type. The AbsoluteUri property returns a string which is a value type, and default(string) is null, therefor the entire expression evaluates to null.

Can you confirm this is the behavior you are seeing on your end? Does this happen if you assert the expression to be null? I could not recreate the expression you get, but that may be due to test framework differences.

Thanks,
Doron
Typemock Support
answered by doron (16.5k points)
0 votes
Hi

yes that is the case but that behaviour brakes isolation and ReturnRecursiveFakes .

i would expect to write no more code than Isolate.Fake.StaticMethods<HttpContext>(Members.ReturnRecursiveFakes); and isolate all static calls to httpContext
answered by tolisss (28.8k points)
0 votes
Hi Tolisss,

* If the value type is not part of MSCorLib you will recieve a fake back from the property call.

* Because most of the value types in .NET cannot be NULL and so the result of default(T) would return a real object (if it is part of MSCorLib).

* string is a special case - because it is part of MSCorLib currently we cannot fake it. in any other the result would be a Fake<T> as you might have expected.

The following code would work like you expected it:
struct StructUnderTest
{
    public int IntValue { get; set; }
}

class ClassWitStructMember
{
    public StructUnderTest StructValue { get; set; }
}

[TestMethod]
public void RecursiveFakesAndValueType()
{
    var classUnderTest = Isolate.Fake.Instance<ClassWitStructMember>(Members.ReturnRecursiveFakes);
    
    Assert.IsNotNull(classUnderTest.StructValue.IntValue);
}


Isolator development team would look into adding special behavior to string but right now it would return its default value which is NULL.
answered by dhelper (11.9k points)
...