Unit testing expressions with null coalescing using c# and Sonarcube

How do you do this at all, using c# and Sonar?

Given the code:

if (root?.hop1?.hop2.hop3?.value == "blah") // this condition is not covered as far as Sonar is concerned
{
  do(); // this line is covered as far as Sonar is concerned
} 

we can create an object to test do(), but how do you cover the actual condition? Supplying just a “happy” path leaves the condition uncovered, should we supply all the permutations constituting unhappy paths (which is a nightmare as this code is in the middle of another test) or how do you do it otherwise?

19

What you can do is to define parameter for test method and provide it ClassData attribute with type of data generator, which would produce all combinations.

Considering such setup:

public class Innermost
{
    public string Value { get; set; }
}
public class Inner
{
    public Innermost Innermost { get; set; }
}
public class Outer
{
    public Inner Inner { get; set; }
}
public class Outermost
{
    public Outer Outer { get; set; }
}

public static class ClassUnderTest
{
    public static string? GetString(Outermost o)
    {
        if(o?.Outer?.Inner?.Innermost?.Value == "value")
        {
            return "value";
        }
        return "";
    }
}

You could define data generator in separate class:

public class TestDataGenerator : IEnumerable<object[]>
{
    public IEnumerator<object[]> GetEnumerator()
    {
        var outerMost = new Outermost();
        yield return [outerMost];

        outerMost = new Outermost();
        outerMost.Outer = new Outer();
        yield return [outerMost];

        outerMost = new Outermost();
        outerMost.Outer = new Outer();
        outerMost.Outer.Inner = new Inner();
        yield return [outerMost];

        outerMost = new Outermost();
        outerMost.Outer = new Outer();
        outerMost.Outer.Inner = new Inner();
        outerMost.Outer.Inner.Innermost = new Innermost();
        yield return [outerMost];

        outerMost = new Outermost();
        outerMost.Outer = new Outer();
        outerMost.Outer.Inner = new Inner();
        outerMost.Outer.Inner.Innermost = new Innermost();
        outerMost.Outer.Inner.Innermost.Value = "random string";
        yield return [outerMost];
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

And then use it inside test:

public class UnitTest1
{
    [Theory]
    [ClassData(typeof(TestDataGenerator))]
    public void Test1(Outermost outerMost)
    {
        var result = ClassUnderTest.GetString(outerMost);

        Assert.Equal("", result);
    }
}

This way all data generation will be separated in different class. There you could also leverage AutoFixture package to further simplify object creation in unit tests.

3

My answer is – in a situation when an expression containing “?” is evaluated to a value or checked with a standalone if branch it’s completely unnecessary to demand for the values representing “early exits”, it adds nothing but the hustle.

I’d suggest to refactor SUT code to use something like target.Evaluate<T>("hop1.hop2.hop3").

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật