Closed Selvaticus closed 8 years ago
This does not appear to be valid usage of Smocks: your Setup
does not target a method or property. Note that the syntax for Smock is heavily inspired by Moq. To get to grips on how to use Smocks properly, it's therefore recommended to read a tutorial on Moq. Also check out the examples in Smock's readme.
Also note that Smocks is intended to cover some edge cases that are impossible to mock using other frameworks, such as Moq. I'm not sure if the code provided here is such an edge case: it might be simpler to use Moq instead.
Hi @vanderkleij
I'm using Moq as well, the test case I want to test is an extension method on a public static class
, that uses other methods on the same class which are private static
, and in turn this methods use the ViewContext.RouteData
. It's possible to mock ViewContext
, but not RouteData
as it doesn't have any virtual members, and you can't inject any route values because the properties are read only.
That's why I thought Smocks could help me. Am I looking at this wrong? Do you know of a better way?
Cheers
Thanks for the clarification. That sounds like a use case for Smocks indeed. What you should do exactly depends on the code that you're testing, but let's assume that it's accessing RouteData.Values
. You could then supply mocked values to the code under test like this:
Smock.Run(context =>
{
RouteValueDictionary routeValues = new RouteValueDictionary();
routeValues.Add("Foo", "Bar");
context.Setup(() => It.IsAny<RouteData>().Values).Returns(routeValues);
return new ViewContext().IsOnController("TestController");
});
@Selvaticus: do you need any further help, or can we close this issue?
Sorry will try it out today, and let you know.
Sent from my Windows Phone
From: Tom van der Kleijmailto:notifications@github.com Sent: 05/04/2016 12:42 To: vanderkleij/Smocksmailto:Smocks@noreply.github.com Cc: Diogo Silvamailto:DiogoSilva_406@Hotmail.com Subject: Re: [vanderkleij/Smocks] Can't run tests (#19)
@Selvaticus: do you need any further help, or can we close this issue?
You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/vanderkleij/Smocks/issues/19#issuecomment-205766651
I'm having the same error message. I'm just trying to simply test the framework:
[TestMethod] public void HelloSmocksTest() { Smock.Run(context => { context.Setup(() => DateTime.Now).Returns(new DateTime(2015, 2, 20)); }); }
I have reference "Smocks" in my .Net4 c# proj.
Any idea Why?
@vanderkleij
My code is indeed just accessing the RouteData.Values
on the ViewContext
. So your snippet looks exactly what I want. But I've tried your snippet and I still get the same error.
No code is ever run, the error comes from the Smocks.Run()
as far as I know.
Both my web app and the test projects are running 4.5.2
Stack trace:
Mono.Cecil.AssemblyResolutionException : Failed to resolve assembly: '<MYTESTDLL>'
at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name,
ReaderParameters parameters)
at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name)
at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
at Mono.Cecil.MetadataResolver.Resolve(MethodReference method)
at Mono.Cecil.MethodReference.Resolve()
at Smocks.IL.MethodDisassembler.Disassemble(MethodBase method)
at Smocks.IL.Dependencies.DependencyGraphBuilder.BuildGraphForMethod(MethodBase method)
at Smocks.Smock.CreateAssemblyRewriter(Delegate delegate, Configuration configuration)
at Smocks.Smock.RunFunc[T](Func'2 func, Configuration configuration)
at Smocks.Smock.RunInternal[T](Func'2 func, Configuration configuration)
at Smocks.Smock.Run(Func'2 func)
at <MYTESTDLL>.Extensions.ViewContextExtensionsTests.IsOnControllerTest()
I am unable to reproduce these issues. Both my example and the simple example posted by @pars87 run fine over here.
I'm testing with Smocks 0.6.29 in Visual Studio 2015 with .NET 4.5.2 on Windows. @Selvaticus and @pars87: what does your environment look like?
The code does indeed work in an isolated solution(sln). but when i try to import the project to other solution(nothing more) i do get the error. i have spend all day to find out WHY, but i just cant :(.
however i do not have the same issue with other dlls, like moq
. its only smock
.
I really really need to mock DateTime
. any idea how otherwise?
I'm using Smocks 0.6.29, Vs 2012 in .Net 4 on windows 8
I think the problem is Mono.Cecil.dll
I will need a minimal, complete example that illustrates the problem in order to resolve it. Perhaps you can put together a (minimal) complete solution that exhibits the issue, and upload that somewhere?
I'm running Smocks 0.6.29 in Visual Studio 2015 Update 1 with .NET 4.5.2 on Windows. Also have the .NET 4.5.1 and 4.6 installed on the machine.
I was trying to create an minimal complete example, when I found out that if you use NUnit 3.2.0
I get the error I described above, but if i use the Microsoft.VisualStudio.TestTools.UnitTesting
no error.
Did I overlook something?
I can upload the example, but it is literally just change the using
statments, and you reproduce the error.
I'm not a 100% sure on how to recreate the problem from your description, so it would be convenient if you could upload the minimal solution that illustrates the problem.
Thanks for the example. I'm able to reproduce the issue now and I'll resolve it in the near future.
Status update: due to some career changes, I haven't been able to dedicate a lot of time to Smocks the past few months. I hope to find some more time to resolve issues shortly.
Smocks only seems to work with NUnit 2, not with even the lowest 3.x version.
Would be good to have this fixed.
Thanks @vijuhe for pointing me in the right direction. Mono Cecil was unable to resolve referenced assemblies while running in the AppDomain of NUnit3 tests. To resolve this, I've added a (Cecil) IAssemblyResolver
that explains Cecil where to look for the assemblies. Using Smocks in NUnit 3 tests should now work.
The fix is available on nuget now.
I updated the package and also NUnit (now using the latest 3.5). Unfortunately now all the tests fail with a different exception:
System.NullReferenceException
Smocks.IL.Dependencies.DependencyGraphBuilder.DependencyGraphInstructionVisitor.VisitMethod(Instruction instruction, MethodReference operand)
Smocks.IL.Visitors.InstructionVisitorBase1.VisitInlineMethod(Instruction instruction, MethodReference operand) Smocks.IL.InstructionExtensions.Accept[T](Instruction instruction, IInstructionVisitor
1 visitor)
Smocks.IL.Dependencies.DependencyGraphBuilder.BuildGraphForMethod(MethodBase method)
Smocks.Smock.CreateAssemblyRewriter(Delegate delegate, Configuration configuration)
Smocks.Smock.RunAction(Action1 action, Configuration configuration) Smocks.Smock.Run(Configuration configuration, Action
1 action)
Smocks.Smock.Run(Action`1 action)
Thanks for retesting and providing feedback. Do you have a small failing test that you can share, so I can reproduce the issue?
Hi,
I'm having some trouble testing some really old legacy code, and I wonder if you could assist me.
Whenever i try to run a simple test i get this error
Mono.Cecil.AssemblyResolutionException : Failed to resolve assembly: '<MyTestDll>'
The code throwing the error is as simple as this:
i'm new to Smocks, so I can be missing something but it just seems strange that it fails resolved the actual test dll.
Any help will be greatly appreciated.
Cheers