0

I am trying to test a domain model for a SaaS application with FsCheck and XUnit. Each model entity has a column TenantId with some unique tenant identifier. There are numerous Asserts checking that TenantIds are not mixed up.

For now, I have this TenantId generated with Gen.Constant generator. However, this is fixing the value for the whole test run, so all tests are using the same TenantId. It would be more convenient if this value would be reset for each test but stay constant within a test.

To be more specific, let's say that there are two entities and the class for TenantId:

public record TenantId(Guid value);

public record EntityA(TenantId TenantId, string SomeString);

public record EntityA(TenantId TenantId, int SomeNumber);

And some xUnit tests annotated with [Property]:

[Property]
public void PropertyTest1(EntityA a, EntityB b) => Assert(a.TenantId == b.TenantId);

[Property]
public void PropertyTest2(EntityA a, EntityB b) => Assert(a.TenantId == b.TenantId);

Is it possible to configure an Arbitrary for TenantId type so it will different values for PropertyTest1 and PropertyTest2, but constant value inside these tests?

Michael Korbakov
  • 2,147
  • 1
  • 18
  • 20
  • Can't test if it's what you want, but you could try `Gen.Fresh` instead of `Gen.Constant`, this will generate a "fresh" instance every time it's called, instead of returning the same one. – Kurt Schelfthout Apr 21 '21 at 13:41
  • Unfortunately, it is not working. I extended the original question with more details. – Michael Korbakov Apr 21 '21 at 16:02
  • In my view if entityA and B need to have the same TenantId within a particular test, then the usual way of achieving that would be to generate them together. Presumably in your domain they are also part of a larger structure, not clear how you enforce this constraint otherwise outside tests. One way of achieving something like it without that is to make each property depend on TenantId, string and int, and then create EntityA and B directly in the test (then it's obvious they have the same id). – Kurt Schelfthout Apr 25 '21 at 10:50

0 Answers0