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.

Is it possible to have a mix of loose and strict modes?

See original GitHub issue

I’d like mock to behave in a way described in Mark’s article

I’d consider as a command any method returning void/Task/ValueTask, query - for anything else. So, whenever commands are called, I can verify the call, and when query is performed, the exception is thrown unless the result is explicitly defined.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Oct 20, 2021

Ah, I see!

1reaction
blairconradcommented, Oct 20, 2021

Hi, @voroninp. There’s nothing like that built into FakeItEasy itself, and I’m not sure if there’s widespread demand, but it wouldn’t be too hard to configure Fakes in this way. One could make the Fake strict and then override the strictness for the methods one cared about. Here’s a very rough attempt, whose tests pass:

namespace FakeItEasyQuestions
{
    using System.Threading.Tasks;
    using FakeItEasy;
    using FluentAssertions;
    using Xunit;

    public class Test
    {
        public interface IInterface
        {
            int AQuery();
            void ACommand();
        }

        [Fact]
        public void QueryIsStrict()
        {
            var fake = AHalfStrictFake<IInterface>();
            var exception = Record.Exception(() => fake.AQuery());
            exception.Should().NotBeNull();
        }

        [Fact]
        public void CommandIsLoose()
        {
            var fake = AHalfStrictFake<IInterface>();
            fake.ACommand();
            A.CallTo(() => fake.ACommand()).MustHaveHappened();
            
        }

        private static T AHalfStrictFake<T>() where T: class
        {
            var fake = A.Fake<T>(o => o.Strict());
            A.CallTo(fake).WithVoidReturnType().DoesNothing();
            A.CallTo(fake).WithReturnType<Task>().DoesNothing();
            return fake;
        }
    }
}

Of course there are many ways to optimize this. For example, one could apply the configuration via an implicit creation option. Or possibly by adding an IFakeOptions extension. This is how CallsBaseMethods is implemented, in fact.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Moq, strict vs loose usage
I used to use strict mocks when I first starting using mocks in unit tests. This didn't last very long. There are really...
Read more >
Loose mode vs strict mode loop detection : r/networking
My 2nd question is between strict mode and loose mode, it seems like strict mode will only block loops that would happen if...
Read more >
What is the difference between RPF strict and RPF loose mode?
RPF Loose: RPF loose mode looks for a existence of a route to the source in the routing table. RPF check will pass...
Read more >
Basic Wireless Settings - DD-WRT Wiki
Available Settings: Off, 802.11h Loose, 802.11h Strict, 802.11d ... In Mixed mode, dd-wrt routers are able to offer various wifi network ...
Read more >
Understand the Extended Ping and Extended Traceroute ...
The normal ping works both in the user EXEC mode and the privileged EXEC mode. ... (Loose Source Routing, Strict Source Routing, Record...
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