Question

How to get UserConnection when writing Unit Test classes

Hi, 

 

I created a unit test class following the instructions here https://academy.creatio.com/documents/technic-sdk/7-16/testing-tools-nunit for a custom class I created in Creatio.

 

However, in order to test I need to pass the UserConnection to my class. How do I logon and get the UserConnection from Terrasoft.Configuration.Tests?

 

Thanks in advance,

Jose

Like 0

Like

5 comments

Thanks for the information. I will try this.

Hi, how can I mock filter behaviour in my tests? For Example, I have method: And simple test: esq always returns 2 value, so is it possible to change this and mock filtered result? Thank you in advance

Oleksii Ozerov, Try this
AddFilter to esq mock (github.com)

You can see how data is mocked in BaseMarketplaceTestFixture class
 

Oleksii Ozerov,

 

One of the options is to mock using the DBExecutor:

  1. 1) To set a system setting value in a test, add the system setting key and its value to the TestUserConnection.SettingsValues ​​property (class Dictionary<string, object>)
var appConnection = Substitute.For&lt;AppConnection&gt;();
var userConnection = new TestUserConnection(appConnection);
var dbExecutor = Substitute.For&lt;DBExecutor&gt;(userConnection);

2) Configuring UserConnection to work correctly without a database

userConnection.DBExecutor = dbExecutor;
userConnection.DBEngine = Substitute.For&lt;DBEngine&gt;();
userConnection.DBTypeConverter = new TestDBTypeConverter();

3) Prepare the results table structure by forming the list of result columns and result records

var testData = new TestData()
   .Structure()
       .AddStringColumn("Code")               
       .AddBoolColumn("CanExecute")
   .NewRow("CanManageAdministration", false)   
   .NewRow("CanManageData", true);
 
DataTable dataTable = testData.GetDataTableUseDataStructure();
// or testData.DataStructure;

Example: Mock the request to DB, which contains "SysAdminOperation" and parameter value = "CanManageAdministration"

dbExecutor.ExecuteReader(ArgExt.Contains("SysAdminOperation"), ArgExt.ContainsQueryParameterByValue("CanManageAdministration"))
   .Returns(dataTable.CreateDataReader());

P.S. It is recommended that the GetTestDataWithStructure method be used in the UnitTestUtilities class so as not to worry about manually forming the sampling structure. Example:

TestData testData = UnitTestUtilities.GetTestDataWithStructure(_testEntitySchemaManager, "Activity");
var customValues = new Dictionary&lt;string, object&gt; {
    {"Sender", email}
};
testData.DataStructure.AddRowWithCustomColumnValues(customValues);
Show all comments