patterncsharpModerate
Does this FizzBuzz code correctly follow SRP and unit-testing?
Viewed 0 times
thissrpandfizzbuzztestingfollowdoescodecorrectlyunit
Problem
This is code for FizzBuzz. I have also hosted in on my GitHub.
FizzChecker Tests
Fizz
Buzz tests
FizzChecker Tests
[TestClass]
public class FizzChecker_Tests
{
IFizzChecker _fizzChecker;
bool isFizz;
[TestInitialize]
public void TestInitialize()
{
//Arrange
_fizzChecker = new FizzChecker();
isFizz = default(bool);
}
[TestMethod]
public void When_Devisible_By_Three_Return_True()
{
//Act
isFizz = _fizzChecker.IsFizz(3);
//Assert
Assert.AreEqual(isFizz, true);
}
[TestMethod]
public void When_Not_Devisible_By_Three_Return_False()
{
//Act
isFizz = _fizzChecker.IsFizz(1);
//Assert
Assert.AreEqual(isFizz, false);
}
[TestMethod]
public void When_Devide_By_Zero_Return_True()
{
//Act
isFizz = _fizzChecker.IsFizz(0);
//Assert
Assert.AreEqual(isFizz, true);
}
}Fizz
public interface IFizzChecker
{
bool IsFizz(int number);
}
public class FizzChecker : IFizzChecker
{
public bool IsFizz(int number)
{
return (number % 3) == 0;
}
}Buzz tests
[TestClass]
public class BuzzChecker_Tests
{
BuzzChecker _buzzChecker;
bool isFizz;
[TestInitialize]
public void TestInitialize()
{
//Arrange
_buzzChecker = new BuzzChecker();
isFizz = default(bool);
}
[TestMethod]
public void When_Devisible_By_Fize_Return_True()
{
//Act
isFizz = _buzzChecker.IsBuzz(5);
//Assert
Assert.AreEqual(isFizz, true);
}
[TestMethod]
public void When_Not_Devisible_By_Three_Return_False()
{
//Act
isFizz = _buzzChecker.IsBuzz(1);
//Assert
Assert.AreEqual(isFizz, false);
}
[TestMethod]
public void When_Devide_By_Zero_Return_True()
{
//Act
isFizz = _buzzChecker.IsBuzz(0);
//Assert
Assert.AreEqual(isFizz, true);
}
}Solution
I think you're overdoing it. SRP does not mean that every class should have only one method and every method should be a one-liner.
Instead, you should structure your code into parts that are likely to change together (though that's not the only criterion). But here, it's not really possible to guess that, because it's obvious these are not real requirements.
I think that FizzBuzz is a good problem to test that you know the basics of programming (logical way of thinking about problems) or to test that you know the basics of some programming language (ifs, functions). But because of how artificial the problem is, it's not very good for testing things like SRP or unit testing.
Instead, you should structure your code into parts that are likely to change together (though that's not the only criterion). But here, it's not really possible to guess that, because it's obvious these are not real requirements.
I think that FizzBuzz is a good problem to test that you know the basics of programming (logical way of thinking about problems) or to test that you know the basics of some programming language (ifs, functions). But because of how artificial the problem is, it's not very good for testing things like SRP or unit testing.
Context
StackExchange Code Review Q#38920, answer score: 11
Revisions (0)
No revisions yet.