Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

c# - Unit testing with Mocks. Test behaviour not implementation -


i had problem when unit testing classes calls other classes, example have class creates new user phone-number saves database , sends sms number provided.

like code provided below.

public class userregistrationprocess : iuserregistration {     private readonly irepository _repository;     private readonly ismsservice _smsservice;      public userregistrationprocess(irepository repository, ismsservice smsservice)     {         _repository = repository;         _smsservice = smsservice;     }      public void register(string phone)     {         var user = new user(phone);         _repository.save(user);         _smsservice.send(phone, "welcome", "message!");     } } 

it simple class how go , test it?

at moment im using mocks dont it

    [test]     public void whenregistreringanewuser_thenewuserissavedtothedatabase()     {         var repository = new mock<irepository>();         var smsservice = new mock<ismsservice>();         var userregistration = new userregistrationprocess(repository.object, smsservice.object);          var phone = "07012345678";          userregistration.register(phone);         repository.verify(x => x.save(it.is<user>(user => user.phone == phone)), times.once());     }      [test]     public void whenregistreringanewuser_itwillsendanewsms()     {         var repository = new mock<irepository>();         var smsservice = new mock<ismsservice>();         var userregistration = new userregistrationprocess(repository.object, smsservice.object);          var phone = "07012345678";          userregistration.register(phone);          smsservice.verify(x => x.send(phone, it.isany<string>(), it.isany<string>()), times.once());     } 

it feels testing wrong thing here?

any thoughts on how make better?

refactoring mocks out in way @serghei suggests good.

i see name of behaviour isn't describing behaviour. use word "should", in, "my class should stuff".

your class shouldn't send user database when it's registering user. should ask repository save user. that's all. doesn't know whether repository sends database, keeps in memory or nukes orbit. it's not class's responsibility.

by phrasing behaviour way, can explicitly show - , others understand - scope of class's responsibility ends.

if rename method whenregisteringanewuser_asksrepositorytosaveit() might make example you've given feel more natural.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -