0

I have a base abstract class that exists in a nuget package that I am implementing a concrete class for, something like this:

public class MyConcreteClass : MyBaseAbstractClass<SomeType> {

  public MyConcreteClass(IAnInterfaceOne interfaceOne, IAnInterfaceTwo interfaceTwo) : base(interfaceOne, interfaceTwo) { }

}

I am mocking the two parameters like so:

var mockFirst = new Mock<IAnInterfaceOne>();
var mockSecond = new Mock<IAnInterfaceTwo>();

and the concrete class like so:

var mockConcrete = new Mock<MyConcreteClass>(mockFirst.Object, mockSecond.Object);

When I try to access the Object property within mockConcrete

var concreteObjectInstance = mockConcrete.Object

I get a FileNotFoundException, Could not load file or assembly 'Serilog.dll' and the debugger in VS points to the base constructor call for where the exception is coming from:

base(interfaceOne, interfaceTwo)

Castle.Core and Moq versions:

<package id="Castle.Core" version="3.3.3" targetFramework="net452" />
<package id="Moq" version="4.5.21" targetFramework="net452" />

I have searched through the nuget package where the base class resides in and there are no references or usages of serilog. I searched Castle.Core's repo and found many usages of serilog which I believe is a logging library. So I am a bit confused on how this exception is occuring and how I can try to resolve it so it does not throw the exception when I mock my object.

httpNick
  • 2,524
  • 1
  • 22
  • 34
  • Is your concrete class the system under test or are you using it as a dependency for the actual system under test? – Nkosi Sep 21 '16 at 19:20
  • The concrete class is a dependency I want to mock a return value for. What I am actually testing uses it, but yeah it is just a dependency. – httpNick Sep 21 '16 at 19:21
  • Does it also fail if you new up the concrete class manually and pass the two mocks. Since it is already concrete I don't see what you want to mock it given that it will already be using mocks of it's dependencies `var mockConcrete = new MyConcreteClass(mockFirst.Object, mockSecond.Object);` – Nkosi Sep 21 '16 at 19:24
  • So the reason I do not want to manually create the concrete and pass in the two mocks is because it has a method that hits a service. I want to mock the return value of that method so it doesn't go out and hit the service. – httpNick Sep 21 '16 at 19:29
  • Then your problem is design. The service is a dependency and should then also be abstracted so that you can mock it. As it currently stands your concrete class is tightly coupled to that service. That server should be injected. – Nkosi Sep 21 '16 at 19:30
  • So MyBaseAbstractClass has the virtual method that reaches the service. `public virtual TOutboundContract ServiceCallMethod(RestArguments arguments = null, bool forceBypassOfCache = false);`, can't virtual methods from abstract classes be mocked? – httpNick Sep 21 '16 at 19:35
  • Yes. Yes they can. The dependence on that concrete service is still a design issue but I see your point. – Nkosi Sep 21 '16 at 19:39

1 Answers1

0

After digging through I found the cause of the problem. It actually is not related to castle or moq. It was actually caused by Metrics.NET which is a nuget package being referenced in MyBaseAbstractClass<T>. This line was in the base class:

private readonly Metrics.Timer _callTimer = Metric.Timer("", Unit.Calls, SamplingType.LongTerm, TimeUnit.Milliseconds);

and the exception was being thrown every time the base class was initialized on that line. I looked at the packages Metrics.NET was referencing and saw LibLog. So I looked into LibLog and saw it was referencing Serilog. So I am not sure why Serilog cannot be found within those packages still, but I removed the line throwing the exception since it was unused anyways and also removed the Metrics.NET package. I am answering my question to make sure people know it is not a problem with Castle or Moq like I originally thought it was.

httpNick
  • 2,524
  • 1
  • 22
  • 34