-1

The following error is:

Errors and Failures: 1) Test Failure : fizzbuzz.FizzBuzzTest.TestInputFifteen Expected string length 8 but was 4. Strings differ at index 0. Expected: "FizzBuzz" But was: "Buzz" -----------^

at fizzbuzz.FizzBuzzTest.TestInputFifteen () [0x00000] in :0

2) Test Failure : fizzbuzz.FizzBuzzTest.TestInputOneHundredAndFive Expected string length 8 but was 4. Strings differ at index 0. Expected: "FizzBuzz" But was: "Buzz" -----------^

at fizzbuzz.FizzBuzzTest.TestInputOneHundredAndFive () [0x00000] in :0

Dan
  • 11
  • 2
  • Please put the errors *in the question*. I'd also strong suggest that you learn about data-driven tests - the code is the same in each test, so you could just use `[TestCase(...)]` to make that better. – Jon Skeet Mar 04 '16 at 06:59
  • @DmitryBychenko: I don't see why, to be honest... – Jon Skeet Mar 04 '16 at 07:00
  • I believe the problem is probably just the lack of `{` after the namespace declaration in the second file. It certainly doesn't feel like it's an error which is really fizzbuzz or nunit-specific. – Jon Skeet Mar 04 '16 at 07:00
  • Considering the error messages you've posted refers to `FizzBuzz.cs`, and the class here is named `FizzBuzzTest` I think it is highly likely the errors you're seeing is unrelated to the test code but instead related to the code under test. Can you post the contents of `FizzBuzz.cs` (instead) ? – Lasse V. Karlsen Mar 04 '16 at 07:10
  • @LasseV.Karlsen I was given a Test and I had to write code to pass the test. The FizzBuzz.cs that you are talking about is the solution contents – Dan Mar 04 '16 at 07:16
  • OK, now that you've posted the actual file then the answer here has already been edited to show the actual problem, there is a bracket missing. In future posts, please post the file that is being complained about, even though you may believe the problem to be elsewhere. – Lasse V. Karlsen Mar 04 '16 at 07:17

1 Answers1

0

The immediate cause of the error is that you've omitted { after namespace name:

  namespace fizzbuzz { // <- this "{"
    public class FizzBuzz {    
      ...
    }
  } // <- and this "}"

However even if you amend this typo, you'll face another ones (you can't create an instance of a static class, FizzBuzz hasn't required constructor etc.); let's start from a test, say this one:

[Test]
public void TestInputOneHundred() {
    FizzBuzz fizzbuzz = new FizzBuzz(100);
    Assert.AreEqual("Buzz", fizzbuzz.ToString());
}

you're creating FizzBuzz isntance and then call ToString(). So you have to implement something like this:

   namespace fizzbuzz { // <- do not forget "{"
      // Not static! You (== your test) want to create instances
      public class FizzBuzz {  
        // create, passing int (exactly as test wants)
        public FizzBuzz(int value) {
          Value = value;
        }

        // ToString will want the value 
        public int Value {get; set;}

        // ToString to call in the test
        public override ToString() {
          if (Value % 5 == 0)  
            return "Buzz";

          return Value.ToString(); 
        }
      }
   }

And the test passed. Run another one, e.g.

   [Test]
    public void TestInputThree() {
        FizzBuzz fizzbuzz = new FizzBuzz(3);
        Assert.AreEqual("Fizz", fizzbuzz.ToString());
    }

To pass this one you have to modify ToString() into

public override ToString() {
  if (Value % 5 == 0)  
    return "Buzz";
  else if (Value % 3 == 0) // for the 2nd test  
    return "Fizz"; 

  return Value.ToString(); 
}

And so on until all the test are passed.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • He's getting compiler errors, the actual question is hidden in a comment. – Lasse V. Karlsen Mar 04 '16 at 07:09
  • @Dan, sorry for typo, it should be `public override String ToString()`, since `ToString` returns, of course, `String` – Dmitry Bychenko Mar 04 '16 at 07:20
  • @Dmitry Bychenko so I got the last test, but now I am stuck on "FizzBuzz" as an output – Dan Mar 04 '16 at 07:42
  • @Dan: well, the condition is pretty clear: *hen divisible by both 3 and 5...*, so all you have to put is `if ((Value % 5 == 0) && (Value % 3 == 0)) return "FizzBuzz";` on the top of `ToString()` – Dmitry Bychenko Mar 04 '16 at 07:45
  • @Dmitry Bychenko Yes I did, but I think when the number should appear I messed that up i edited the updated code – Dan Mar 04 '16 at 07:49
  • @Dmitry Bychenko and its still giving me an error for the return "FizzBuzz" – Dan Mar 04 '16 at 07:50
  • @Dan: `if ((Value % 5 == 0) && Value % 3 == 0)) return FizzBuzz`should be *on the top of* `ToString()`, i.e. *first* check for *FizzBuzz* and only then for *Fizz* and *Buzz* separately – Dmitry Bychenko Mar 04 '16 at 07:52
  • @Dmitry Bychenko oh my bad got it – Dan Mar 04 '16 at 08:04
  • @Dmitry Bychenko one last thing so after doing all the test there is thisYou should consider boundary conditions when you write your code. FizzBuzz is not defined for numbers less than 1. In this case, you should return the string "Undefined" in any case where the number given is < 1. – Dan Mar 04 '16 at 08:07
  • @Dan and boundary conditions should be placed *before* even *FizzBuzz*: since in case of, say, `-100` you want `"Undefined"` not `"FizzBuzz"` – Dmitry Bychenko Mar 04 '16 at 08:10
  • @DmitryBychenko like this if (Value % =< -100) return "Undefined"; – Dan Mar 04 '16 at 08:15
  • @Dan: `if (Value < 1) return "Undefined";` - just as you've said: *in any case where the number given is < 1* – Dmitry Bychenko Mar 04 '16 at 08:19
  • @DmitryBychenko really appreciate your help – Dan Mar 04 '16 at 08:22