Real World Testing with Xamarin.Forms – Red Green Refactor with Better Names

In this lesson

  • Improve variable names for better clarity


Tap on time to skip ahead


Now, the one thing I want to do in this video is practice the refactor phase of the Red Green Blue or the Red Green Refactor process of TDD. You’ll see that I’m using a lot of explicit strings here. It would be nice if instead, I were to use variables that would help clue another developer in, in what I’m doing.


With that in mind, I’m going to take this “letmein” string. I’m going to create a new local variable. I’m going to name this ValidPassword. Then, I’m going to make this a field for my test suite. I’m going to set up the initialization, the field declaration. I always make my fields protected by default.


Now, what I can do is I can go into my other test and replace password with ValidPassword. I’m going to also do that in this one here. Then, I can also do the same thing to our email. I’m going to say ValidEmailAddress. Copy and paste that. Make it a field.


Then lastly, we’re going to replace the bad password again with a field. We’re going to name this InvalidPassword. I guess we should make these all constants, right? What this does is it kind of helps clue other developers in what’s going on. I can now say, “FakeUserService.Authenticate ValidEmailAddress ValidPassword.” Or up here, I can say, “ValidEmailAddress InvalidPassword” and thus, help clue them in on what we’re trying to do.


Again, one of the things that I stress when writing unit tests is how readable is my test code. The more readable and easier your test code is to understand, the less likely your tests are going to be thrown out down the road or broken by other people.