Sunday 15 May 2011

c# - How would I refactor out a static method so I can test my method? -


I know that I do not use the Moq to simulate a static method call within my method under test So, what do I need to do to refactor so that I can test it? I have a way to call the base class method, I have to re-apply it, and if so how? I do not want to use MS.Fakes or TypeMocks and to make a shim, I will not write the refactor and the solid code!

  Public override datetime resolve date (ISERS expansions, date time target date) {if (ComparisonSeries == faucet) {New argument NullException ("comparisons"); } Switches (comparisons. KI) {Case Series of R. 1: Case Series R. 2: Case Series KR R3: Case SeriesRR R4: Case Series Key R. 5: Return Death Helicopter Curriculum (Target Date); } Return Base Resolve date (comparison, target date); } [Testimede] Public Zero Some Testimonials () {varMokiaApache = New Fake & lt; IAppCache & gt; (); Var MockArisis = New Fake & lt; ISeries & gt; (); ReportFR2 report = new ReportFR2 (SeriesKeyFR2, Mokiapack cache); DateTime resolved date = report Resolve date (fake series, datetime); //Assert.AreEqual("Something "," Something "); }    

Looking at the above, you have three basic conditions that you can test

  • When the comparison series The key is not empty but there is anything but R1: R5

    In position 1, you can easily cover it with a test

    In condition 2, when it is R1: R5, it seems that it hits your static method.

    • From the perspective of your ResolveDate method, you still notice that what is the value when it kills the branch. It would be better to be able to prove that R1: R5 has called a stable assistant, but as stated in the comments, the only way to do this is to have your datehelter with an interface. Wrap and pass the creator of this class. It may not be worth the effort.
    • If you decide not to do so, then I suggest that you can still give an examination that falls in that branch, and still your datehaller The former or the Crural Quarterend () function kills all edge cases directly, it does not matter if the code fails, it will help to isolate the code.

      The condition can also be said as condition 2. Although it is in your base class, it is still a valid logic arm

    • Again, it will be a difficult time to prove that it is called your base class, / P>

    • But it is still valid to check the result.

      1. Looking at a ReportRF2 class and a null Comparison series ul >

      2. While calling the report. Resolve

      3. Use this should throw a null reference exception. Use `` Assert.Throws ((=) => Reports. Resolve date (empty, datetime, now)) <

      4. A series key in the report RF2 class and R1: R5

        • Border X ( Like 1/1/0001) when solving a date, it should be equal;

        • When "..." for ..., ...; (Repeat for cases on your edge / border, consider using data driven)

        • The report is in the set of RF 2 square and R1 Looking at the chain key: R5 and NOT faucet

          • When the date for the boundary X is being resolved ... is similar to # 2, but possibly different expected results.
          • In cases # 2

            This will give you expected result coverage, and tell you that the failures are increasing with your ResolveDate method or your Dateplaner Former or Criminal Quarterend () Method This will not be as different as a Purist as much as you want, but as long as you cover your growing issues and your happy path, it proves that your app is working in a planned form (Unless these examinations are going on). > It does not really allow you to do this, it claims that a specific behavior is taken, when the series of comparisons is zero, other than that, so it is up to you to decide that you Whether that verification is required or not, you should still have proof that when there are some values ​​or categories, you get the estimated output, and whatever values ​​it adds.

  • No comments:

    Post a Comment