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.

Improve interface of Invokes to allow specifing number of calls

See original GitHub issue

Currently it is not possible to directly specify a number of calls when configuring Invokes, e.g. given the following interface:

public interface ITest
{
	int Test(int testNumber);
}

a configuration like:

[Test]
public void Test()
{
	var counter = 0;

	var test = A.Fake<ITest>();

	A.CallTo(() => test.Test(1))
		.Invokes((int testNumber) => counter++)
		.Once();
}

does not compile.

However, all it takes is a cast and it works flawlessly:

[Test]
public void Test()
{
	var counter1 = 0;
	var counter2 = 0;

	var test = A.Fake<ITest>();

	((IAfterCallConfiguredConfiguration<IReturnValueConfiguration<int>>)A.CallTo(() => test.Test(1))
		.Invokes((int testNumber) => counter1++))
		.Once()
		.Then
		.Invokes(() => counter2++);

	test.Test(1);
	test.Test(1);
	test.Test(1);

	Assert.That(counter1, Is.EqualTo(1));
	Assert.That(counter2, Is.EqualTo(2));
}

I suppose this is unintended and the number of calls specification should be available directly, without a cast.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Dec 30, 2020

Perhaps it could be solved by adding types for rules - a rule would be applied if no rule of the same was already applied. That way each new configuration of Returns would overwrite previous ones (same type), but Invokes would apply independently (different type).

Unfortunately, that would be a major breaking change that would impact every current user of FakeItEasy, just to solve a problem very few people have…

I’m sorry I seem so reluctant to accept your suggestions. They’re not necessarily bad ideas, but we need to be very careful when evolving the library. Many users rely on it, and we try to minimize breaking changes. When we do introduce a breaking change, it’s usually:

  • either a behavior change (without API change) to fix a behavior that we consider to be a bug (so it’s unlikely anyone relies on it)
  • or an API change that will require the user to change their code in order to be able to compile (so they can’t ignore the change)

The change you propose is the worst kind of breaking change : no API change (so existing code will keep compiling without error), but a major behavior change (so existing code will suddenly start to fail at runtime without any apparent reason).

0reactions
blairconradcommented, Mar 15, 2021

This change has also been released as part of FakeItEasy 7.0.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Design pattern for interface that determines order of ...
I want to create a Java interface with a number methods. But I want the user of the interface to only be able...
Read more >
Interface defining a constructor signature?
Interfaces define contracts that other objects implement and therefore have no state that needs to be initialized.
Read more >
Explicit Interface Implementation - C# Programming Guide
An explicit interface implementation is a class member that is only called through the specified interface. Name the class member by ...
Read more >
How to make thread-safe calls to controls - Windows Forms ...
Invoke method to call a delegate created in the main thread, which in turn calls the control. Or, implement a System.ComponentModel.
Read more >
Invoke - AWS Lambda
Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. By default, Lambda invokes your function ......
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