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
Im brand new to TypeMock.

When I run this test I get an exception that says:

TypeMock.TypeMockException :
*** Faking behavior on struct System.Data.SqlTypes.SqlString is not supported in the following cases:
1. Faking behavior on more than one struct of the same type.
2. Faking behavior on more than one method on the same struct.

How do I do I correct this for my code?

Here is my Code:

using System;
using System.Data.SqlTypes;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

namespace Arh.Database.SqlFunctions.Tests
{
// ReSharper disable InconsistentNaming

[TestClass]
public class UserDefinedFunctionsTest
{
[Isolated]
[TestMethod]
public void GetExpectedArgParams_ArgNIsNullAndArgNDelimiterIsNull_ReturnsNull()
{
//Arrange – setup for the test
Isolate.Fake.StaticConstructor(typeof(SqlString));

var format = Isolate.Fake.Instance<SqlString>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => format.IsNull).WillReturn(true);

var argN = Isolate.Fake.Instance<SqlString>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => argN.IsNull).WillReturn(true);

//Act – the actual invocation of the method
var userDefinedFunctions = UserDefinedFunctions.FormatString(format, argN, null);

//Assert – verification of pass/fail condition for the test
Assert.IsNull(userDefinedFunctions);
}
}

// ReSharper restore InconsistentNaming
}
asked by keithbeller (1.1k points)

1 Answer

0 votes
Hi Kieth,

First - welcome to Typemock!

Typemock works best where you need to fake classes. With value types (such as SqlString) that you're trying to fake, it gets tricky. The support for structs is partial, since there are no struct instances. You can, as the message say, use the APIs for faking an "instance" and methods only once.

This is one of those rare cases of Isolator, where you actually need to change your code for the test. The best way is to introduce a wrapper class, like SqlStringWrapper that wraps the SqlString you want to change behavior to, and pass it instead to UserDefinedFunctions.FormatString. With classes, and therefore real instances, Isolator has no problem discerning which instance is which, and the rest of your test will run as you've written it.

For example:

public class SqlStringWrapper
{
SqlString instance;
public SqlStringWrapper(SqlString newString)
{
instance = newString;
}

public bool IsNull
{
get { return instance.IsNull; }
}
}

And then, change the signature of FormatString() to accept the SqlStringWrapper instances.
I'll put the missing feature on our backlog, so we won't forget it.

Please let me know if this helped, and if you got your test working.

Gil Zilberfeld
Typemock support team.
answered by gilz (14.5k points)
...