Telerik Forums
JustMock Forum
1 answer
24 views

In my test I am using both MockingContainer and Mock to do Automocking and create concrete mocks respectively.

At the end of my test I want to ensure all of the functions I setup via Arrange for both the MockingContainer and mocked classes were ran.

Do I need to call .AssertAll() for each mocked object and the MockingContainer or can I simply call Mock.AssertAll() to make sure all Arranged functions were executed? See example below for both approaches.

// Establish mocks
var mockedControllerContainer = new MockingContainer<SomeController>();
var mockedClass = Mock.Create<SomeClass>();

// Establish test variables
IEnumerable<SomeDomainModel> records = new List<SomeDomainModel>();

// Arrange Mocks
mockedControllerContainer
    .Arrange<Repository>(repo => repo.FetchRecordsWithName("name"))
    .Returns(records);

mockedClass
    .Arrange(c => c.SomeFunction())
    .Returns(null);

// Assert
mockedControllerContainer.AssertAll();
mockedClass.AssertAll();

// Or can I use the example below to cover all cases?

Mock.AssertAll();

Which approach is best practice to ensure each function was called? This example only had one concrete class but other tests can have many so if I can avoid having to use .AssertAll() for each of them individutally that would be ideal.

The API documentation for Mock.AssertAll() states "Asserts all expected setups in the current context". How is this context defined?

Thank you!

Ivo
Telerik team
 answered on 21 Mar 2024
1 answer
37 views

JustMock supports .ReturnsAsync() when arranging standard mocks, however I am unable to find a way to get this same behavior when Automocking with a MockingContainer<T>.

A solution I've found is to wrap the return value in Task.FromResult<T>(). This fulfills the expected Task<T> return type of the async function, however the syntax is a bit clunky.

service .Arrange<IRepository>(r => r.SomeAsyncMethod("input")) .Returns(Task.FromResult<ISomeModel>(resultModel));

Ideally, I would like to avoid building the Task<T> return values manually and just rely on .ReturnsAsync() to handle the conversion.

service .Arrange<IRepository>(r => r.SomeAsyncMethod("input")) .ReturnsAsync(resultModel);

It's possible the APIs just aren't there, but I want to ensure I'm not just missing some special syntax to get it working.

Tsvetko
Telerik team
 answered on 03 Jan 2024
0 answers
205 views

I want the Mock to return the SQL statement and Not execute the statement. This is what i have done but it keeps running the "connection.execution(SQL)".  

                         

 

string SQL = "";

            Mock.Arrange(() => connection.Execute(Arg.AnyString, null, null, null, null)).DoInstead((string arg1) => { SQL = arg1; }).IgnoreInstance().Returns(1);



            WebApiProject.Controllers.DeleteController dc = new WebApiProject.Controllers.DeleteController();
           
            dc.Post(mockedDeleteObject);

            Assert.IsTrue(SQL == $"DELETE FROM TEST WHERE 1=1");

 

 

Post{

                string SQL;
                SQL = $"delete from {TableName}  where  {WhereClause} ";

                con.Execute(SQL,parameters );}

 

 

 

But it keeps running the sql executable statement

 

 

 

Ope
Top achievements
Rank 1
 asked on 03 Mar 2022
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
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?