2011-11-14 14 views
8
//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); 

ऊपर उल्लिखित कोड अपवाद फेंकने के लिए NSubstitute का उपयोग कैसे करें।एक आलसी वर्ग

lazily-प्रारंभ प्रकार @ sanosdole की टिप्पणी के अनुसार एक सार्वजनिक, parameterless निर्माता

मैं सी # 4.0 और उपयोग कर रहा हूँ NSubstitute 1.2.1

+1

क्या वास्तव में आलसी को प्रतिस्थापित करना चाहते हैं? मैं बस यह मानता हूं कि आलसी <> वर्क्स फैक्ट्री कन्स्ट्रक्टर का उपयोग करता है, सबस्टिट्यूट प्रदान करता है। () के लिए वैल्यू फैक्ट्री के रूप में ... – sanosdole

+0

@ sanosdole की टिप्पणी में +1। उस उत्तर को एक समुदाय विकी के रूप में पोस्ट किया है। –

उत्तर

9

नहीं है, मैं एक का उपयोग कर सुझाव है वास्तविक विकल्प Lazy उदाहरण आपके विकल्प को वापस करने के लिए। कुछ ऐसा:

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

target.SendNotify("Message"); 

notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null); 
संबंधित मुद्दे