1

I'm trying to mix CallsBaseMethod and CallTo and it's not calling the one I have setup. Please see the code below and my comments. Is there a way to get this to work or a different approach with FakeItEasy?

public LayoutManager(ICompanyManager companyManager)
{
    this._companyManager = companyManager;
}

this.CompanyManagerFake = A.Fake<ICompanyManager>();
// using StructureMap, put this here to make the example more brief, in my code it's in a base class
 ObjectFactory.Configure(registry =>
 {
   registry.For<ICompanyManager>().Use(this.CompanyManagerFake);
});
this._layoutManager = A.Fake<LayoutManager>();
var layouts = GetTestLayouts();

// I want to get the actual GetLayoutForUser method
A.CallTo(() => this._layoutManager.GetLayoutForUser(A<int>.Ignored)).CallsBaseMethod();

// I want to mock the data for the GetAll method (which is called in GetLayoutForUser)
A.CallTo(() => this._layoutManager.GetAll(A<string>.Ignored)).Returns(layouts.AsQueryable());
A.CallTo(() => this.CompanyManagerFake.GetAll(A<string>.Ignored)).Invokes(
    call =>
    {
        // this doesn't get called from GetLayoutForUser, but is from the line below
        var x = call.Arguments;     
    });

// I want to use .Returns(new List<Company>().AsQueryable()); instead of Invokes, but needed to set a breakpoint
// this hits the above Invokes as expected
var assignedCompanIds = this.CompanyManagerFake.GetAll()
    .Where(c => c.UserProfiles.Any(up => up.UserId == 123)
            || c.UserProfiles1.Any(up => up.UserId == 123))
    .Select(c => c.CompanyId);

 // Act
 var result = this._layoutManager.GetLayoutForUser(123);

 // Assert
 // something

Note: I also put this question on GitHub This seems similar to this question, but I can't put it together. So when I call

var assignedCompanyIds = this._companyManager.GetAll()
                .Where(c => c.IsAssigned)
                .Select(c => c.CompanyId).ToList();

I get this exception: {X} method threw exception: System.ArgumentException: Expression of type '' cannot be used for parameter of type 'System.Linq.IQueryable1[Company]' of method 'System.Linq.IQueryable1[Company] Where[Company](System.Linq.IQueryable1[Company], System.Linq.Expressions.Expression1[System.Func`2[Company,System.Boolean]])'

AlignedDev
  • 8,102
  • 9
  • 56
  • 91
  • I am currently experiencing this as well. – Rick Rat Jun 14 '13 at 19:00
  • @Aligned, I'm having a terrible time following this question. Do you have a simpler example that exhibits the problem? (Alternatively, does it still happen for 1.23.0?) – Blair Conrad Aug 11 '14 at 02:19
  • @BlairConrad I added a comment to the GitHub issue and closed it. I'm not going to be able to find time to boil this down better. Thanks for looking at it. – AlignedDev Aug 11 '14 at 14:42

1 Answers1

0

I fixed my issue by marking my methods I want to be called public instead of internal, because apparently the [assembly: InternalsVisibleTo("TestProject")] was not working correctly.

Rick Rat
  • 1,732
  • 1
  • 16
  • 32
  • Interesting. So you made them public so you could mock them with FakeItEasy (A.CallTo(() => ...)? – AlignedDev Jun 17 '13 at 18:33
  • Yeah! I removed the InternalsVisibleTo and made the method public instead of internal, it worked. Once I re-built, cleaned and re-added internals visible, and re-made it internal, it worked again. – Rick Rat Jun 18 '13 at 20:56
  • In my case, the functions were already public. Thanks for your answer, maybe it will help someone else. – AlignedDev Jun 19 '13 at 12:57