Reputation: 9478
Here's a simple code which reproduces an exception:
var csbMock = new Mock<DbConnectionStringBuilder>();
var csb = csbMock.Object;
csb.ConnectionString = "data source=.;integrated security=true";
Debug.WriteLine(csb.ConnectionString); // System.NullReferenceException in System.Data.dll
Stack frame consists of only one line: at System.Data.Common.DbConnectionStringBuilder.get_ConnectionString()
My goal is to simply be able to get/set DbConnectionStringBuilder.ConnectionString
property of the mock. What's the problem here?
Upvotes: 1
Views: 343
Reputation: 247018
There is actually no need to mock the DbConnectionStringBuilder Class.
There are no knock-on effects of using the class so you could create an instance of it and use it as is
var builder = new DbConnectionStringBuilder();
//Set the connection string directly
builder.ConnectionString = "data source=.;integrated security=true";
//or add the parts needed to create a connection string
builder.Clear();
builder.Add("integrated security", true);
builder.Add("Data Source", ".");
Upvotes: 2
Reputation: 9478
This line fixed the problem:
csbMock.CallBase = true;
Some magic happens inside DbConnectionStringBuilder.ConnectionString
setter. It calls virtual members which are overriden by a proxy and then this NRE hapens in getter.
Upvotes: 0