11
votes
//Assert
Lazy<INotificationService> notificationService = Substitute.For<Lazy<INotificationService>>();
Service target = new Service(repository, notificationService);

//Act
target.SendNotify("Message");

//Arrange
notificationService.Received().Value.sendNotification(null, null, null, null);

The above code throws an exception.

The lazily-initialized type does not have a public, parameterless constructor

I am using C# 4.0 and NSubstitute 1.2.1

1
Do really want to substitute the Lazy<Xyz>? I would just assume that Lazy<> works und use the Value Factory constructor of it, providing Substitute.For<Xyz>() as Value Factory...sanosdole
+1 to @sanosdole's comment. Have posted that answer as a community wiki.David Tchepak

1 Answers

12
votes

As per @sanosdole's comment, I would suggest using a real Lazy instance to return your substitute. Something like:

var notificationService = Substitute.For<INotificationService>();
var target = new Service(repository, new Lazy<INotificationService>(() => notificationService));

target.SendNotify("Message");

notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null);