question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicitly allowing default property behaviour on a strict Fake

See original GitHub issue

The default read/write behaviour of a faked property is very useful, however, it seems that there is no way to allow it on a strict Fake by explicitly calling any available configuration method.

A solution could be adding a generalization of the DoesNothing method (UsesDefaultBehaviour ?), so allowed calls could be configured like this:

var i = A.Fake<IInterface>(o => o.Strict());

A.CallTo(() => i.Property)
	.UsesDefaultBehaviour();
A.CallToSet(() => i.Property)
	.UsesDefaultBehaviour();

It would be even more convenient if a dedicated method wrapping both getter and setter was also added:

var i = A.Fake<IInterface>(o => o.Strict());

A.CallToProperty(() => i.Property)
	.UsesDefaultBehaviour();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, May 5, 2020

@PiotrKlecha, I don’t really understand how you wouldn’t know when to change a method’s behaviour, since you would know if you are making a call to change the behaviour between test phases.

It’s a bit of an abuse, but worst case, you could check a flag inside a WhenArgumentsMatch matcher (https://fakeiteasy.readthedocs.io/en/stable/argument-constraints/#overriding-argument-matchers)

A.CallTo(() => fake.Bar)
    .WhenArgumentsMatch(args => shouldStillDoNonStandardBehaviour)
    .Throws<Exception>();

And then flip the flag when you want the behaviour to revert.

Regardless, I think this still seems like a very very edge case, and making use of the existing tools is a better solution than adding such a niche operation to the main library.

0reactions
PiotrKlechacommented, May 5, 2020

@blairconrad, I meant that a test using a limited call specifier like Once is more likely to fail in an unexpected way if the production code changes and calls the configured method a different number of times. Or it may not detect that additional calls are not properly caught, because they will not throw exceptions at all.

The suggested ‘abuse’ actually seems to be quite reasonable. Too bad it won’t help with strict Fake property configuration 😞

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to properly use default values for properties on ...
no, you have to explicitly allow undefined to be passed as well. In strict mode, boolean and boolean | undefined are different types...
Read more >
Specifying a Fake's Behavior - FakeItEasy Succinctly Ebook
Simply put, specifying Strict on any created fakes forces you to configure them. Any calls to unconfigured members throw an exception. Let's ...
Read more >
Default fake behavior
Fake objects come with useful default behavior as soon as they are created. ... Methods and properties can only be faked if they...
Read more >
Apache Tomcat 8 Configuration Reference (8.5.92) - System ...
Introduction. The following sections list the system properties that may be set to modify the default Tomcat behaviour. Property replacements ...
Read more >
Binding to Data Services with Spring Boot in Cloud Foundry
Use the Spring Boot default autoconfiguration and declare the cloud bindings using application.properties (or in YAML). To take full advantage ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found