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
Hello,

I'm in the process of evaluating TypeMock. I've made some tests using Isolator and they run fine the first time. Every time I run them afterwards they always fail complaining about a NullException. I'm facking calls to the SqlConnection class to make it throw an error:


[TestMethod, Isolated]
public void TestConnectionPassesMock()
{
CachingService fakeCachingService = CreateMockCachingService();

var fakeSqlConnection = Isolate.Fake.Instance<System>();
Isolate.Swap.AllInstances<System>().With(fakeSqlConnection);
Isolate.WhenCalled(() => fakeSqlConnection.Open()).IgnoreCall();

var accessor = CachingService_Accessor.AttachShadow(fakeCachingService);
accessor.TestConnection();
}



Error:
Test method VehiclesServiceLayer.Tests.Services.TypeMockTests.TestConnectionPassesMock threw exception: System.NullReferenceException: Object reference not set to an instance of an object..

Stack Trace:
System.Data.Common.DbConnectionOptions.ValidateKeyValuePair(String keyword, String value)
System.Data.Common.DbConnectionStringBuilder.set_Item(String keyword, Object value)
System.Data.SqlClient.SqlConnectionStringBuilder.SetValue(String keyword, String value)
System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
System.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
CachingService.TestConnection() in
<my>


Any idea what's going on?

PS: there's a typo in the mail confirmation for the registration ;)
asked by R4cOOn (760 points)

8 Answers

0 votes
After further investigation, the following test method fails:


[TestMethod, Isolated]
public void MyTest()
{
var fakeSqlConnection = Isolate.Fake.Instance<System>();
Isolate.Swap.AllInstances<System>().With(fakeSqlConnection);
Isolate.WhenCalled(() => fakeSqlConnection.Open()).IgnoreCall();

SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.ConnectionString = StaticData.ConnectionString;
connectionStringBuilder.ConnectTimeout = 15;

SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connectionStringBuilder.ConnectionString;
sqlConnection.Open();
}


Test method TypeMockTests.MyTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object..

System.Data.Common.DbConnectionOptions.ValidateKeyValuePair(String keyword, String value)
System.Data.Common.DbConnectionStringBuilder.set_Item(String keyword, Object value)
System.Data.SqlClient.SqlConnectionStringBuilder.SetValue(String keyword, String value)
System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
System.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
TypeMockTests.MyTest() in (the line is this one connectionStringBuilder.ConnectionString = StaticData.ConnectionString;)
answered by R4cOOn (760 points)
0 votes
Hi,

I am trying to execute the test you posted but i can't find the type of System which is being faked.

Could you please post the type of it?

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
0 votes
Sorry, looks like the post ate part of the namespace. Here it is straight copy/pasted from the code (I verified that it still fails the same way):

[TestMethod, Isolated]
public void MyTest()
{
var fakeSqlConnection = Isolate.Fake.Instance<SqlConnection>();
Isolate.Swap.AllInstances<SqlConnection>().With(fakeSqlConnection);
Isolate.WhenCalled(() => fakeSqlConnection.Open()).IgnoreCall();

SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.ConnectionString = StaticData.ConnectionString;
connectionStringBuilder.ConnectTimeout = 15;

SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connectionStringBuilder.ConnectionString;
sqlConnection.Open();
}



Cheers.
answered by R4cOOn (760 points)
0 votes
Hi,

I tried to reproduce it but I couldn't make it fail.

Are you using latest version of Isoaltor (5.3.1)? What OS are you using? Are you running it in 32 or 64 bit?

By the way, why are you using here SqlConnectionStringBuilder and assigning an actual connection string to the SqlConnection? You can fake the open connection and omit this part of code.

[TestMethod, Isolated]
public void MyTest()
{
    var fakeSqlConnection = Isolate.Fake.Instance<SqlConnection>();
    Isolate.Swap.AllInstances<SqlConnection>().With(fakeSqlConnection);
    Isolate.WhenCalled(() => fakeSqlConnection.Open()).IgnoreCall();

    SqlConnection sqlConnection = new SqlConnection();
    sqlConnection.Open();
}


Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
0 votes
Hello,

I created a separate solution with just this test inside and it fails as described. I'm using the latest version 5.3.1.0 on VS2008 on a 32 bit machine.

The test doesn't fail if I remove the SqlConnectionBuilder faking (it's the line that throws the exception). However in the code that I'm testing I need to fake the instance because the string from which I build it isn't there in the test environment.

Can I send you the test solution or post it somewhere for bug reporting?
answered by R4cOOn (760 points)
0 votes
We can continue investigating this issue on our side just send the repro solution to support at typemock dot com
answered by dhelper (11.9k points)
0 votes
I've just sent the sample solution to support.
answered by R4cOOn (760 points)
0 votes
Hi,

We managed to reproduce it. This is a bug causing this exception.

Workaround to this issue:
In Visual Studio disable MSTest keeping test engine between tests -
Tools -> Options -> Test Tools -> Test Execution -> Performance -> Keep test execution between test runs (uncheck it).

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
...