最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to ensure all properties of object are asserted in test? - Stack Overflow

programmeradmin2浏览0评论

I have class Foo:

class Foo
{
   public string Bar { get; set; }
}

I have lots of tests which reference this class. How I can ensure that all new properties are tested when my system evolves and I extend Foo with new property?

I.e. after some time I will add a new property to Foo class:

class Foo
{
   public string Bar { get; set; }
   public string Baz { get; set; }
}

I need to review all code which tests my class and ensure that all properties are tested - which is manual process, hard to always get right.

I have class Foo:

class Foo
{
   public string Bar { get; set; }
}

I have lots of tests which reference this class. How I can ensure that all new properties are tested when my system evolves and I extend Foo with new property?

I.e. after some time I will add a new property to Foo class:

class Foo
{
   public string Bar { get; set; }
   public string Baz { get; set; }
}

I need to review all code which tests my class and ensure that all properties are tested - which is manual process, hard to always get right.

Share Improve this question edited Feb 2 at 20:57 Basheer Jarrah 3963 silver badges10 bronze badges asked Jan 29 at 23:18 ShadowShadow 2,4786 gold badges30 silver badges60 bronze badges 3
  • 3 You'll likely need to build a Roslyn analyzer that's invoked via a test - or use a code-coverage library. BTW, consider using immutable types with constructors instead of mutable properties: once you make the change you never look back (and then you start learning F#, OCaml and eventually Haskell, and then it's all downhill from there – Dai Commented Jan 29 at 23:20
  • 1 Even if you have mutable immutable type with constructor you still can add property to this type (i.e. in service), and verify result in tests. This does not really solve the problem. – Shadow Commented Jan 30 at 0:13
  • "Baz" introduces one or more new interfaces: IFooBar; IFooBaz; IFooBarBaz. Tells you what and where. – Gerry Schmitz Commented Jan 30 at 1:49
Add a comment  | 

1 Answer 1

Reset to default 0

There is not much you can do actually to guarantee this. You can try writing a Roslyn analyzer to check for assertions but arguably it would be not worth the effort and probably would not be that feasible.

As alternative you can rely on code coverage tools (integrated into the build pipeline) and setting high coverage target (90+ %).

Another option would be just moving assertions to a method (or set of methods) like:

public void AssertFoo(string expectedBar, string expectedBaz)
{
   // 
}

Or

public void AssertFoo(Func<string, bool> assertBar, Func<string, bool> assertBaz)
{
   // 
}

And substitute your assertions to call for such function. And when a new property will be added it would be easy to just modify those functions and code will automatically become incompatible.

发布评论

评论列表(0)

  1. 暂无评论