I discovered an odd anomaly with NSubstitute class instantiation. Working with Sitecore 8.1 update 3 Sitecore.Kernell.dll, the following test passes:
[Fact]
public void CanCreateSubstituteDatabase()
{
Database db = Substitute.For<Sitecore.Data.Database>("sub");
db.Should().NotBeNull();
}
This despite the fact that there is only an internal constructor for Sitecore.Data.Database:
internal Database(string name)
{....
I have confirmed that this is not normal behavior for NSubstitute. I created a project "ExternalLibrary" with this code:
namespace ExternalLibrary
{
public class Foo
{
internal Foo(string bar)
{
Bar = bar;
}
public string Bar { get; }
}
}
When I try to use NSubstitute to instantiate this in a separate library,
namespace NSubClassInstantiation
{
using ExternalLibrary;
using FluentAssertions;
using NSubstitute;
using Xunit;
public class FooTest
{
[Fact]
public void CanInstantiate()
{
var foo = Substitute.For<Foo>("baz");
foo.Bar.Should().Be("baz");
}
}
}
the test fails as expected with the following exception,
System.NotSupportedException: Parent does not have a default constructor. The default constructor must be explicitly defined.
Why is this error not thrown with the Sitecore.Data.Database class?