开发者

PEX: How would you test an equality method in pex?

开发者 https://www.devze.com 2023-01-17 07:09 出处:网络
So i\'m he开发者_高级运维re playing with PEX, it seems like a great idea. However I am having a few problems, such as I have no way to test an equals method using parameter unit tests.

So i'm he开发者_高级运维re playing with PEX, it seems like a great idea.

However I am having a few problems, such as I have no way to test an equals method using parameter unit tests.

Maybe there is no way, maybe its a technique i haven't figured out yet.

Someone must have a decent idea.

If i was doing this in moq for instance, I would ensure that all the properties on both objects are read and do the comparisons myself to verify them. however I do not see how to use this approach with parametarised tests.

the problem is that I need to verify that method calls are made and properties are set / read in my business logic. I have no idea how to do this in PEX and there isnt really a massive amount of documentation out there.


There are some basic properties you can check that are related to the mathematical definition of equality:

  • does not crash: a == b never throws an exception
  • symmetric: (a == b) == (b == a)
  • reflexive: (a == a) == true
  • transitivity: (a == b) && (b == c) ==> a == c
  • given Func f, a == b ==> f(a) == f(b)

All of those are nice but definitely don't guarantee you that the equality works. but some point you will have specify as assertions what equality means for you. For example, that values of Property P should be equal, etc... Ultimately, you will end up with a second specification of the equality as tests.

Things get more interresting when you investiate the relationship with GetHashCode:

  • a.GetHashCode() !+ b.GetHashCode() ==> a != b
  • idempotent: a.GetHashCode() == a.GetHashCode()
0

精彩评论

暂无评论...
验证码 换一张
取 消