svetlova / static-mock Goto Github PK
View Code? Open in Web Editor NEWSMock is opensource lib for mocking static and instance methods and properties.
Home Page: https://svetlova.github.io/static-mock/
License: MIT License
SMock is opensource lib for mocking static and instance methods and properties.
Home Page: https://svetlova.github.io/static-mock/
License: MIT License
Hi, @SvetlovA ,The following exception occurred when using the function of Setup(Type type, string methodName, Action action) to unit test a task function, but these is ok when using the function of generic Setup.
Attached is an example (full project) with the simplest possible test for a scenario when a static method is calling another static method.
Can't make it to mock. The mock is ignored and the original method is executed instead.
Async methods mock throws, on all test execution:
Exit code is -532462766 (Unhandled exception. Unhandled exception. Unhandled exception. Unhandled exception. Unhandled exception. Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at StaticMock.Tests.ReturnsTests.GenericSetupMockReturnsAsyncTests.<>c__DisplayClass5_0.<<TestGenericSetupReturnsWithTestMethodReturnParameterFuncAsync>b__1>d.MoveNext() in D:\MyProjects\static-mock\src\StaticMock.Tests\ReturnsTests\GenericSetupMockReturnsAsyncTests.cs:line 102
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()
If you throws custom exception from CallbackAsync this exception throws on setup
Failed test example:
[Test]
public async Task TestCallbackAsyncInstanceThrows()
{
var instance = new TestInstance();
var originalResult = await instance.TestMethodReturnTaskWithoutParametersAsync();
var expectedResult = 2;
Assert.AreNotEqual(expectedResult, originalResult);
Mock.Setup(() => instance.TestMethodReturnTaskWithoutParametersAsync(), async () =>
{
Assert.ThrowsAsync<Exception>(async () => await instance.TestMethodReturnTaskWithoutParametersAsync());
}).CallbackAsync(() => throw new Exception());
}
After updating to net7 tests throws error:
Exit code is -1073741819 (Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at StaticMock.Hooks.Implementation.HookManagerX64.ApplyHook(System.Reflection.MethodBase)
at StaticMock.Mocks.Callback.CallbackMock.Callback(System.Action)
at StaticMock.Mocks.Implementation.VoidMock.Callback(System.Action)
at StaticMock.Tests.CallbackTests.GenericSetupMockCallbackTests.TestActionCallback()
at System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)
at System.Reflection.MethodInvoker.Invoke(System.Object, IntPtr*, System.Reflection.BindingFlags)
at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
at System.Reflection.MethodBase.Invoke(System.Object, System.Object[])
at NUnit.Framework.Internal.Reflect.InvokeMethod(System.Reflection.MethodInfo, System.Object, System.Object[])
at NUnit.Framework.Internal.MethodWrapper.Invoke(System.Object, System.Object[])
at NUnit.Framework.Internal.Commands.TestMethodCommand.InvokeTestMethod(NUnit.Framework.Internal.TestExecutionContext)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(NUnit.Framework.Internal.TestExecutionContext)
at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(NUnit.Framework.Internal.TestExecutionContext)
at NUnit.Framework.Internal.Execution.SimpleWorkItem+<>c__DisplayClass4_0.<PerformWork>b__0()
at NUnit.Framework.Internal.ContextUtils+<>c__DisplayClass1_0`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<DoIsolated>b__0(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at NUnit.Framework.Internal.ContextUtils.DoIsolated(System.Threading.ContextCallback, System.Object)
at NUnit.Framework.Internal.ContextUtils.DoIsolated[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Func`1<System.__Canon>)
at NUnit.Framework.Internal.Execution.SimpleWorkItem.PerformWork()
at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread()
at NUnit.Framework.Internal.Execution.WorkItem.Execute()
at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher.Dispatch(NUnit.Framework.Internal.Execution.WorkItem, NUnit.Framework.Internal.Execution.ParallelExecutionStrategy)
at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher.Dispatch(NUnit.Framework.Internal.Execution.WorkItem)
at NUnit.Framework.Internal.Execution.CompositeWorkItem.RunChildren()
at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformWork()
at NUnit.Framework.Internal.Execution.WorkItem.RunOnCurrentThread()
at NUnit.Framework.Internal.Execution.WorkItem.Execute()
at NUnit.Framework.Internal.Execution.TestWorker.TestWorkerThreadProc())
How can make NestedMocking with Different functions?
like this:
public void TestNestedReturnMock()
{
const int expectedParentResult = 2;
const int expectedChildResult = 3;
Mock.Setup(() => TestStaticClass.TestMethodReturn1WithoutParameters(), () =>
{
Mock.Setup(() => TestStaticClass.TestMethodReturnWithParameter(5), () =>
{
var actualParentResult = TestStaticClass.TestMethodReturn1WithoutParameters();
Assert.AreEqual(expectedParentResult, actualParentResult);
var actualChildResult = TestStaticClass.TestMethodReturnWithParameter(5);
Assert.AreEqual(expectedChildResult, actualChildResult);
}).Returns(expectedChildResult);
}).Returns(expectedParentResult);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.