Closed bitbonk closed 6 years ago
Thanks for the suggestion!
My first thought is that it would be nice to factor out the duplication into a method to create each substitute, in which case this starts to look like this.
Your suggestion of using lambdas per item also reminded me of another way to do this while avoiding the problem of requiring an extra variable mentioned in forsvarir's answer:
[Fact]
public void Initialize_BuildsCorrectRibbonTree2()
{
var commands = Substitute.For<IRibbonCommandsProvider>();
commands.GetRibbonCommands().Returns(x => new [] {
Create("tab1", "group1", "name1"),
Create("tab1", "group1", "name1"),
Create("tab1", "group1", "name1")
});
}
Would that get closer to what you want? Or are you keen to avoid any external function definition?
The downsides of this approach are that I do not have intellisense inside the Create
method and the Create
method is yet another thing I would have to declare outside the test itself.
You should still get intellisense inside Create
(and when calling Create
). All the types are known at compile time. Can you explain where intellisense is failing for you?
I agree it would be nice to not have to declare anything outside the test, but in this case i think the ReturnsMany
sample ends up being more verbose than using Create
, which outweighs the advantage of keeping it all in the same test.
Closing as part of a general cleanup of blocked issues. Please re-open if more information comes to light which will let us proceed with this.
In my application under test I have the following interfaces
And in a test the follwing substitution code:
To be able to have a readable arrange part in my test, I had to create the stub
RibbonCommand
class. This should be Nsubstitutes job! But because AFAIK there is actually no typesafe way to do this with NSubstiute that as anywhere as readable as the above solution, I just had to introduce this class.I propose you introduce a
ReturnsMany
extension method with a signaturer that looks something like this:In my example it would be used like this: