11
//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

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
Kuroro
  • 1,841
  • 1
  • 19
  • 32
  • 2
    Do really want to substitute the Lazy? I would just assume that Lazy<> works und use the Value Factory constructor of it, providing Substitute.For() as Value Factory... – sanosdole Nov 14 '11 at 10:34
  • +1 to @sanosdole's comment. Have posted that answer as a community wiki. – David Tchepak Nov 14 '11 at 21:00

1 Answers1

12

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);
David Tchepak
  • 9,826
  • 2
  • 56
  • 68