Telerik Forums
JustMock Forum
2 answers
171 views

Hi, 

I am trying to run the unit test for my .net 6 and want to see the code coverage using "Analyse Code Coverage for all Tests" in VS2022.

it successfully run all all test cases when I "run all Tests" but when I use "Analyse Code Coverage for all Tests" to run and see the code coverage, there are some test case failed throwing ThrowElevatedMockingException as below.

 Message: 
Telerik.JustMock.Core.ElevatedMockingException : Cannot mock 'System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[AppHub.SingleView.AmazonFireStickPromo.Models.AccountPromoToken]] GetAccountPromoTokensAsync(System.String)'. The profiler must be enabled to mock, arrange or execute the specified target.
Detected active third-party profilers:
* {324F817A-7420-4E6D-B3C1-143FBED6D855} (from process environment)
Disable the profilers or link them from the JustMock configuration utility. Restart the test runner and, if necessary, Visual Studio after linking.

  Stack Trace: 
ProfilerInterceptor.ThrowElevatedMockingException(MemberInfo member)
MocksRepository.CheckMethodInterceptorAvailable(IMatcher instanceMatcher, MethodBase method)
MocksRepository.AddArrange(IMethodMock methodMock)
MocksRepository.Arrange[TMethodMock](Object instance, MethodBase method, Object[] arguments, Func`1 methodMockFactory)
<>c__DisplayClass24_0`1.<Arrange>b__0()
ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
NonPublicExpectation.Arrange[TReturn](Object target, String memberName, Object[] args)
PromoCodeManagementServiceTests.GetAllEmailStatusesAsync_WithTokenFromAccount_ExpectedListEmailStatus() line 145
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

I think run all test is successful as I use specify env.runsettings in my project file (attached file). 

but the test run in  "Analyse Code Coverage for all Tests" failed in some methods.

Package I used

    <PackageReference Include="JustMock.Commercial" Version="2023.1.117.1" />

Can someone help me to fix this one ?

Thanks

Minh
Top achievements
Rank 1
Iron
 updated answer on 20 Apr 2023
1 answer
706 views

 public class Sum
    {
        public int GetSum()
        {
            return SumBy2.Add1(10,5);
        }


        public int GetSum2()
        {
            SumBy1 by1 = new SumBy1();
            return by1.Add(10, 5);
        }


        private class SumBy1
        {
            public int Add(int a, int b)
            {
                return a + b;
            }
        }

        private static class SumBy2
        {
            public static int Add1(int a, int b)
            {
                return a + b;
            }
        }
    }

 

Need to create Unit tests for Sum class.

Ivo
Telerik team
 answered on 05 Oct 2021
1 answer
203 views

Hi,

we have a class like this.

 

public class MssqlDbConnection : ISecuredDatabaseConnection, IDisposable
    {
        #region Fields
        private SqlConnection _connection = null;

     }

 

And I am creating Private Mock like this

             SqlConnection Sql = new SqlConnection();
            // Connected Mock
            Mock.NonPublic.Arrange<SqlConnection>(typeof(MssqlDbConnection), "_connection").Returns(Sql);

This Statement causes crash with System.MissingMemberException in just mock

 

Please let me know on this.

 

Regards,

Chandra.

Ivo
Telerik team
 answered on 17 Sep 2021
2 answers
447 views

Error when trying to mock Microsoft.ServiceFabric.Services.Client with the following code:


using Microsoft.ServiceFabric.Services.Client;

[Fact]

public void Foo() {

var spr = Mock.Create<ServicePartitionResolver>();

}

When I run this simple test I got:


Message: 
System.TypeLoadException : Could not load type 'System.Runtime.Remoting.Proxies.RealProxy' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

  Stack Trace: 
MocksRepository.Create(Type type, MockCreationSettings settings)
<>c__38`1.<Create>b__38_0()
ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
Mock.Create[T]()

 

NovoPath
Top achievements
Rank 1
Iron
 answered on 15 Sep 2021
1 answer
211 views

Hi

I have a function:

    Public Function UpdatePasswordHistory(ByVal mods As DataSource, ByVal StaffCode As String, ByVal Password As String) As Integer


        Dim strSQl As New StringBuilder("Insert into [dbo].[PasswordHistory]([StaffCode],[Password],[DateSet])")
        strSQl.Append($"values('{StaffCode}','{EncryptPassword(UCase(Password))}','{System.DateTime.Today.ToString("yyyy-MM-dd")}')")


        Return mods.SqlDB.ExecuteNonQuery(strSQl.ToString())

    End Function

 

I want to create 2 tests.

The staffcode is a foreign key so must exists.

If no issues it should return 1. else 0.

My 2 tests always return 0.

I probably done this all wrong

   public void UpdatePasswordHistory_Staffcodeexists()
        {
            // Arrange
      var mockDS = Mock.Create<DataSource>();
            clsSecurity clsSecurity = Mock.Create<clsSecurity>();

            string Staffcode = "sahmed";
            string password = "test";
            Int32 rowsaffetced = 1;
            Int32 actualRowsaffected;

            Mock.Arrange(() => clsSecurity.UpdatePasswordHistory(mockDS, Staffcode, password)).Returns(rowsaffetced);

            actualRowsaffected = new clsSecurity().UpdatePasswordHistory(mockDS, Staffcode, password);

            Assert.AreEqual(rowsaffetced,actualRowsaffected);
        }

        [TestMethod]
        public void UpdatePasswordHistory_Staffcodedoesnotexist()
        {
            // Arrange
            var mockDS = Mock.Create<DataSource>();
            clsSecurity clsSecurity = Mock.Create<clsSecurity>();

            string Staffcode = "InvalidUser";
            string password = "test";
            Int32 rowsaffetced = 0;
            Int32 actualRowsaffected;

            Mock.Arrange(() => clsSecurity.UpdatePasswordHistory(mockDS, Staffcode, password)).Returns(rowsaffetced);

            actualRowsaffected = new clsSecurity().UpdatePasswordHistory(mockDS, Staffcode, password);

            Assert.AreEqual(rowsaffetced, actualRowsaffected);
        }

 

Ivo
Telerik team
 answered on 15 Jun 2021
Narrow your results
Selected tags
Tags
+? more
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?