Click here to Skip to main content
15,913,854 members
Home / Discussions / C#
   

C#

 
GeneralRe: Mocks... I don't get it. Pin
Pete O'Hanlon22-May-08 11:47
mvePete O'Hanlon22-May-08 11:47 
GeneralRe: Mocks... I don't get it. Pin
Spacix One22-May-08 11:57
Spacix One22-May-08 11:57 
AnswerRe: Mocks... I don't get it. Pin
Pete O'Hanlon22-May-08 11:43
mvePete O'Hanlon22-May-08 11:43 
GeneralRe: Mocks... I don't get it. Pin
martin_hughes22-May-08 11:59
martin_hughes22-May-08 11:59 
GeneralRe: Mocks... I don't get it. Pin
Pete O'Hanlon22-May-08 22:31
mvePete O'Hanlon22-May-08 22:31 
GeneralRe: Mocks... I don't get it. Pin
martin_hughes22-May-08 23:23
martin_hughes22-May-08 23:23 
AnswerOT Pin
Vikram A Punathambekar22-May-08 19:13
Vikram A Punathambekar22-May-08 19:13 
AnswerRe: Mocks... I don't get it. [modified] Pin
Roger Alsing23-May-08 0:36
Roger Alsing23-May-08 0:36 
It depends on what mocking framework you use.

Some use runtime subclassing + implementing interfaces.

Others are more hardcore and relies on .NET profiling API to intercept the jitter and add their own code to any (sealed / non virtual members) concrete class / static functions etc.

While it is true that it is wise to use interfaces when practising TDD, its not that uncommon that you get a codebase that is not created using TDD and thus you might be forced to create tests that uses those concrete classes.
In such case you are either forced to do some (alot) refactoring of the codebase before you start writing tests, or you can use a mock framework that are able to mock those concrete classes.

And that is just a matter of taste / religion.
Im personally against the concept of altering sealed / non virtual methods because there is a reason they were designed that way in the first place.

However, I can see the benefit of it in cases where you have an existing codebase..
But Im a bit too paranoid to trust that the mangled code will work correctlt just because it wasnt intended to be modified.

---

Also, your example is not how it is intended to be used.
You are supposed to use the mocks when interacting with other, non mocked components.

eg:

//EDIT CP code blocks are crazy today...

var mockery = new Mock[IPerson]();
mockery.Expect(x => x.FirstName).Returns("Martin");

FooComponent foo = new FooComponent();
var res = foo.DoStuff(123,444,55,mockery);

assert.AreEqual(res , 123 , "incorrect value from foo.DoStuff");

//if this fails, then the FooComponent did not interact with the mock as expected
assert.xxxx ( whatever way you test if the mock methods was called );


This way you can see if your test subject interacts correctly with the mock.

In this kind of case, it _can_ be useful to mock concrete classes.
To force it to return something instead of running its own code.
Just so you can see that your test subject interacts with the mock.


modified on Friday, May 23, 2008 6:50 AM

GeneralRe: Mocks... I don't get it. Pin
martin_hughes23-May-08 0:48
martin_hughes23-May-08 0:48 
Questionhow to add blank line Pin
netJP12L22-May-08 11:24
netJP12L22-May-08 11:24 
AnswerRe: how to add blank line Pin
#realJSOP22-May-08 11:34
professional#realJSOP22-May-08 11:34 
AnswerRe: how to add blank line Pin
Anthony Mushrow22-May-08 11:37
professionalAnthony Mushrow22-May-08 11:37 
AnswerRe: how to add blank line Pin
Pete O'Hanlon22-May-08 11:38
mvePete O'Hanlon22-May-08 11:38 
GeneralRe: how to add blank line Pin
netJP12L22-May-08 11:59
netJP12L22-May-08 11:59 
GeneralRe: how to add blank line Pin
Pete O'Hanlon22-May-08 22:28
mvePete O'Hanlon22-May-08 22:28 
AnswerRe: how to add blank line Pin
Christian Graus22-May-08 13:41
protectorChristian Graus22-May-08 13:41 
QuestionInhertited set of controls with default implementation Pin
Benny Jaminé22-May-08 11:14
Benny Jaminé22-May-08 11:14 
QuestionProblem with deployment Pin
Shuaib wasif khan22-May-08 8:47
Shuaib wasif khan22-May-08 8:47 
AnswerRe: Problem with deployment [modified] Pin
Peter Josefsson Sweden22-May-08 10:03
Peter Josefsson Sweden22-May-08 10:03 
GeneralRe: Problem with deployment Pin
Shuaib wasif khan22-May-08 10:29
Shuaib wasif khan22-May-08 10:29 
GeneralRe: Problem with deployment Pin
Peter Josefsson Sweden22-May-08 11:09
Peter Josefsson Sweden22-May-08 11:09 
GeneralRe: Problem with deployment Pin
Peter Josefsson Sweden22-May-08 11:19
Peter Josefsson Sweden22-May-08 11:19 
QuestionProblem with Access database connectivity in C#.Net Pin
Verghese22-May-08 6:47
Verghese22-May-08 6:47 
AnswerRe: Problem with Access database connectivity in C#.Net Pin
Gareth H22-May-08 7:02
Gareth H22-May-08 7:02 
GeneralRe: Problem with Access database connectivity in C#.Net Pin
Verghese22-May-08 8:48
Verghese22-May-08 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.