Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Apr 01 2016 16:24
    jeremymeng synchronize #617
  • Apr 01 2016 14:28
    blairconrad commented #490
  • Apr 01 2016 11:07
    blairconrad commented #629
  • Mar 31 2016 14:14
    PascalK starred FakeItEasy/FakeItEasy
  • Mar 31 2016 13:00
    blairconrad commented #490
  • Mar 31 2016 12:48
    drauch commented #490
  • Mar 31 2016 10:57
    blairconrad commented #617
  • Mar 31 2016 10:52
    adamralph commented #617
  • Mar 31 2016 10:42
    blairconrad commented #617
  • Mar 31 2016 10:41
    blairconrad commented #617
  • Mar 31 2016 10:29
    adamralph commented #617
  • Mar 31 2016 09:27
    adamralph commented #637
  • Mar 31 2016 08:29
    cecilphillip commented #637
  • Mar 31 2016 06:27
    M-Zuber commented #637
  • Mar 31 2016 06:25
    thecodejunkie commented #637
  • Mar 31 2016 06:25
    adamralph commented #637
  • Mar 31 2016 06:24
    adamralph commented #637
  • Mar 31 2016 06:13
    adamralph labeled #637
  • Mar 31 2016 06:13
    adamralph labeled #637
  • Mar 31 2016 06:13
    adamralph assigned #637
nitinmuch
@nitinmuch

using (IServiceScope scope = _serviceProvider.CreateScope())
{

        }   

I am not able to fake above code
It throws cast exception

9 replies
Danut Alexandru
@Rock3rRullz_twitter
Hi. I want to test the following line but I didn't manage to find a solution for it. Any ideas? return gatheredFramesProfiles.Any() ? null : gatheredFramesProfiles?.Where(x => x.Parent.Equals(parent)).ToList(); All I did manage to do is to return null, but how could I return something that matches the action defined in where ?
Thomas Levesque
@thomaslevesque
Hi @Rock3rRullz_twitter, what is the type of gatheredFramesProfiles? Is it a fake?
Danut Alexandru
@Rock3rRullz_twitter
@thomaslevesque Yes, it is fake.
Thomas Levesque
@thomaslevesque
Of what type?
Anyway, you won't be able to fake the behavior of Where, because it's an extension method, so it's static.
If gatheredFramesProfiles is a collection: it's not very common to fake a collection, because configuring its behavior is cumbersome. You're usually better off using a real collection whose content you control.
If it's an IQueryable, you can also use a collection and use AsQueryable to convert the collection to an IQueryable
Blair Conrad
@blairconrad
Tal McMahon
@TalMcMahon
This should be easy but I just cant find it in the docs. I have a .net core 3.1 class class that reads a value from Appsettings.json I have to test logic based on a setting in that json. I thought I could fake IConfiguration, and then my code of config["myAppsettingsKey"] would be specified in "A.CallTo" but cannot seem to figure out how to do that
Blair Conrad
@blairconrad
Hi, @TalMcMahon. You're trying to fake the response to config["myAppSettingsKey"], to provide your own value?
Like A.CallTo(() => config["myAppSettingsKey"]).Returns("FakeItEasy is Cool")?
Tal McMahon
@TalMcMahon
Yeah, I may have been overthinking it.
I just implemented this without Fakeiteasy
var myConfiguration = new Dictionary<string, string>();
_fakeConfig = new ConfigurationBuilder()
.AddInMemoryCollection(myConfiguration)
.Build();
Blair Conrad
@blairconrad
And sometimes a hand-rolled fake is good enough!
Or I guess in this case hand-rolled actual.
Tal McMahon
@TalMcMahon
yeah, maybe, what is the real answer if it isn't too much to ask
was it as simple as the above?
Blair Conrad
@blairconrad
Oh. I thought the thing I typed would work. Not quite in front of a compiler/runner, but can check.
Tal McMahon
@TalMcMahon
no problem. I can try, I was hung up on the returns part.
Where I was blowing up is what if the setting wasn't set...
or the config wasn't there
Blair Conrad
@blairconrad

I'm not sure what "config wasn't there" means. I don't think you mean config the variable...

There should be no problem if there isn't a preconfigured value for "myAppSettingsKey". You'd just be telling the fake "if someone calls the this method with the argument "myAppSettingsKey", return "FakeitEay is Cool"". (I don't know why I uppercased Cool)

I just tried the code I put above (admittedly using a hand-rolled interface, not the proper IConfiguration), and it worked perfectly...
namespace FakeItEasyQuestions
{
    using System;
    using FakeItEasy;

    public class Program
    {
        static void  Main(string[] args)
        {
            Test();
            System.Console.Out.WriteLine("Test passed!");
        }

        public static void Test()
        {
            var config = A.Fake<IConfiguration>();

            A.CallTo(() => config["myAppSettingsKey"]).Returns("FakeItEasy is Cool");

            var result = config["myAppSettingsKey"];

            if (result != "FakeItEasy is Cool") {
                throw new Exception(result);
            }
        }

        public interface IConfiguration {
            string this[string key] { get; set; }
        }

    }
}
Tal McMahon
@TalMcMahon
Yup, I was about to type the same.
What I meant by the config isn't there, This is a base class that is deriving in other places, and in those places I didn't want to have to chain the testing...but as I type this...
I guess, if I want it to be there for a base class, then the tests need to know that it needs to be there.
Thanks for the nudge.
Blair Conrad
@blairconrad
Anytime. Good luck!
Ajay Gopal
@AjayGopalC
Will fake it easy support older record/ replay paradigm unit tests?
Thomas Levesque
@thomaslevesque
Hi @AjayGopalC,
Could you elaborate? What exactly do you mean by that?
Do you mean something like that? https://github.com/blairconrad/SelfInitializingFakes
Ajay Gopal
@AjayGopalC
Thanks for your reply, we are currently using RhinoMocks but due to lack of support, in .Net5 we need to find an alternative. One concern for us is the legacy unit test methods written using older Record/ Replay style instead of AAA pattern. Wonder if FakeItEasy supports this paradigm. I am new to FakeItEasy , so any help is appreciated.
Blair Conrad
@blairconrad
No, @AjayGopalC, FakeItEasy does not support record/replay. We'd never considered adding such support. In my personal opinion, it leads to less readable tests than AAA.
I'm sorry that's not the answer you were probably hoping for.
Ajay Gopal
@AjayGopalC
@blairconrad Thanks Blair. Appreciate your reply. I totally agree it leads to less readable tests.
Blair Conrad
@blairconrad
Vlad
@eternaleap

Hi all.
Could you please help me with fakeItEasy advanced usage.
I got method (suppose it called "Insert") which takes an argument as parameter. This parameter filled with data (a lot of it).
I want to return this argument as the result of "Insert" .

Thanks in advance.

object.Insert(A<T>._))
.Invokes(obj =>
{
var p = obj.GetArgument<T>(0);
---SET p AS a return value here--
}
Something like that.
Is it possible at all?
Vlad
@eternaleap
I tried
A.CallTo(() => obj.Insert(A<T>._)).ReturnsLazily((o) =>
It doesn't work too. In debug it didn't stop there.
Hope it's aproppriate place to ask such questions.
Maybe you could suggest another place where I can get help with this issue?
Blair Conrad
@blairconrad

Hi, @eternaleap. This is a perfectly acceptable place to ask!

Alternatives are at the StackOverflow FakeItEasy tag or you could create an issue at the FakeItEasy repository, but here is good. Let's continue.

Your code is a little terse, so there are some details I have to guess at. It might be better to include more, and or/describe exactly what happens, but your second approach sounds good to me. I would've written this code myself:

A.CallTo(() => fake.Insert(A<TypeOfP>._).ReturnsLazily((TypeOfP p) => p));

Where TypeOfP is of course the type of the argument. This is exactly the approach described at Return Values Calculated at Call Time

If this is not working for you, the first thing I'd do is make sure you're configuring the right method. Are you using the correct type in the argument list? Is there an overload of Insert that takes more (or fewer) parameters that you're actually calling in the production code, so the one-parameter version (that takes a TypeOfP) isn't actually being called? Sometimes this is easy to check visually, or sometimes it can help to debug into the code.

If you're able to supply more of your test and production code, or at least method signatures, we might be able to help, if you're still stuck.

Blair Conrad
@blairconrad
Oh, I see you went to StackOverflow. Which is a fine place, but it would've been nice of you to mention that you were cross-posting. I'm going to move my answer and continue there, since it likely has more eyeballs on it.
Vlad
@eternaleap
Thanks a lot. Will try and let you know whether it works or not.
Vlad
@eternaleap
It works. Many thanks for detailed answer.
Blair Conrad
@blairconrad
Yay!