Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. Area of difficulty: coding a class constructor, initially anticipating two constructor arguments, then realising that an extra argument is needed.
  2.  
  3. Example: TDD of a class to encapsulate forename and surname.
  4.  
  5. Prior vision: an immutable class with two constructor arguments: forename and surname, public read-only property for each of these.
  6.  
  7. Up to the point where I realised I wanted another constructor argument, I found TDD of this straightforward:
  8.  
  9. Iteration 1: test that, after construction, the read-only Forename property returns the correct value. By the end of this iteration, the class constructor with both arguments has been coded, but the surname argument is not yet used. I got the test to initially fail by not assigning the Forename value.
  10.  
  11. ```
  12.  
  13. public class Name
  14. {
  15. public Name(string forename, string surname)
  16. {
  17. Forename = forename;
  18.  
  19. }
  20.  
  21. public string Forename { get; private set; }
  22. }
  23.  
  24. ```
  25.  
  26. Iteration 2: test that, after construction, the read-only Surname property returns the correct value. By the end of this iteration, the Surname property has been added and assigned to in the constructor:
  27.  
  28. ```
  29. public class Name
  30. {
  31. public Name(string forename, string surname)
  32. {
  33. Forename = forename;
  34. Surname = surname;
  35. }
  36.  
  37. public string Forename { get; private set; }
  38. public string Surname { get; private set; }
  39. }
  40.  
  41. ```
  42.  
  43. My difficulty arises when I now realise that I would like my class to have an additional ‘Title’ string property (Mr/Miss/Mrs), so needs an extra constructor argument. So, I want the final version of the class to have a single constructor Name(string title, string forename, string surname).
  44.  
  45. My 3rd test is ‘Check that after construction the title property has been stored correctly.’
  46.  
  47. After writing this test, I get it to compile by adding in the 3rd constructor parameter and the read-only property. Like the previous 2 tests, I don’t assign to it in the constructor so as to get the test to initially fail. But... I have to update the constructors in the previous 2 tests to get everything to compile. Does this undermine their integrity? Is the above the correct approach?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement