Note that there are some explanatory texts on larger screens.

plurals
  1. POReflection in TDD
    primarykey
    data
    text
    <p>I have read in a number of places that using reflection to set internal fields during TDD is bad as it probably implies that we have not yet sorted our interface for the Class under test. However I would like to present a scenario which i think is perfectly plausible and I cannot think of any clean solution to the problem at hand except Reflection. </p> <p>Here is an example class with a corresponding test:</p> <pre><code>public Class Movie { public string MovieName{get; private set} public void AddExternalReview(IMovieReview review) { //Logic that needs Unit testing. if ( review.MovieName.Equals(MovieName) ) { //Do stuff.. } } } </code></pre> <p>UnitTest:</p> <pre><code>[Test] public Class MovieTests { public void CanAddExternalReview() { MovieReviewStub movieReviewStub = new MovieReviewStub(); movieReviewStub.MovieName = "The Godfather"; Movie m = new Movie(); //Need to Set object m state(i.e set Property MovieName to "The Godfather") //Only way to do that seems to be relection. m.AddExternalReview(movieReviewStub); //Assert on some condition. } } </code></pre> <p>It is quite common to have properties that cannot be set by a Class's "normal" Clients. However the UnitTest Class may need access to such Properties/Fields so that it can set the relevant state necessary for the Test. As far as I can see reflection is the only way out. This must be quite a common requirement, yet most articles/discussions on the web seem to frown upon the use of reflection in TDD. Any ideas/insights would be most welcome. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload