How to create tests for poco objects

I am new to mocking / testing and want to know what level you should go when testing. For example, in my code, I have the following object:

public class RuleViolation
{
    public string ErrorMessage { get; private set; }
    public string PropertyName { get; private set; }

    public RuleViolation( string errorMessage )
    {
        ErrorMessage = errorMessage;
    }

    public RuleViolation( string errorMessage, string propertyName )
    {
        ErrorMessage = errorMessage;
        PropertyName = propertyName;
    }
}

This is a relatively simple object. So my question is:

Do I need a unit test?

If he does what I test and how?

thank

+5
source share
5 answers

I would say probably not. The only thing you probably want to check if this is extremely important is the access modifiers:

public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }

If it is really really important that the code outside the class cannot change them, this may be the only thing I will try and verify.

Here's how you can get accessors in a property:

class Program
    {
        static void Main(string[] args)
        {
            var property = typeof(Test).GetProperty("Accessor");

            var methods = property.GetAccessors();
        }
    }



    public class Test
    {
        public string Accessor
        {

            get;
            private set;
        }    

    }

WITH property.GetAccessors();

, . , . ( IsPrivate IsPublic, ).

+4

= >

+7

unit test , , . ( NUnit)

[Test]
public void TestRuleViolationConstructorWithErrorMessageParameterSetsErrorMessageProperty() {
    // Arrange
    var errorMessage = "An error message";

    // Act
    var ruleViolation = new RuleViolation(errorMessage);

    // Assert
    Assert.AreEqual(errorMessage, ruleViolation.ErrorMessage);
}

, , , .NET. , Microsoft, : -)

, , , , . Mocking frameworks dependecy, , , , .. Moq - , :

[Test]
public void TestCalculateReturnsBasicRateTaxForMiddleIncome() {
    // Arrange
    // TaxPolicy is a dependency that we need to manipulate.
    var policy = new Mock<TaxPolicy>();
    bar.Setup(x => x.BasicRate.Returns(0.22d));

    var taxCalculator = new TaxCalculator();

    // Act
    // Calculate takes a TaxPolicy and an annual income.  
    var result = taxCalculator.Calculate(policy.Object, 25000);

    // Assert
    // Basic Rate tax is 22%, which is 5500 of 25000.
    Assert.AreEqual(5500, result);
}  

TaxPolicy , , . , TaxCalculator , TaxPolicy, ; TaxPolicy, . // TaxPolicy .

waaaaay Moq, , ; , , .

+1

, , , , . , , , , .

, , , ( , ctor params ? Name ?).

IF (?) . - , , , .

This is the right way to develop code.

NTN,
Berryl

+1
source

Even if it's simple, there is logic in your constructors. I would experience this:

RuleViolation ruleViolation = new RuleViolation("This is the error message");
Assert.AreEqual("This is the error message", ruleViolation.ErrorMessage);
Assert.IsEmpty(ruleViolation.PropertyName);

RuleViolation ruleViolation = new RuleViolation("This is the error message", "ThisIsMyProperty");
Assert.AreEqual("This is the error message", ruleViolation.ErrorMessage);
Assert.AreEqual("ThisIsMyProperty", ruleViolation.PropertyName);
0
source

All Articles