Giter Site home page Giter Site logo

svetlova / static-mock Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 3.0 19.2 MB

SMock is opensource lib for mocking static and instance methods and properties.

Home Page: https://svetlova.github.io/static-mock/

License: MIT License

C# 99.99% PowerShell 0.01%
mock mocking moq static test testing tests unittesting unittests

static-mock's People

Contributors

dependabot[bot] avatar kyohaifeng avatar svetlova avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

static-mock's Issues

Task function unit test exception

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.

キャプチャ

Async mocks throws error

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()

CallbackAsync throws throws Exception on Setup

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());
    }

Error after updating to net7

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 to make NestedMocking with Different functions

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);
    }

An exception occurred when I was testing
error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.