Azure

Creating Unit Checks For Azure Capabilities

Introduction

 

Service Bus Set off Azure features are features that get began each time we obtain a message on the service bus. These features are usually helpful each time we need to get some knowledge from a 3rd celebration or exterior purposes into our purposes. Third-party purposes ship the message to a service bus queue of our utility, so each time a message is obtained within the queue, these features can be triggered and we implement our logic to course of that message.

 

Under is a small perform I’ve created for getting buyer knowledge from different purposes and inserting it into my knowledge.

  1. utilizing Microsoft.Azure.ServiceBus;  
  2. utilizing Microsoft.Azure.ServiceBus.Core;  
  3. utilizing Microsoft.Azure.WebJobs;  
  4. utilizing Newtonsoft.Json;  
  5. utilizing System;  
  6. utilizing System.Textual content;  
  7. utilizing System.Threading.Duties;  
  8. utilizing Area.Repositories;  
  9. utilizing Area.DomainDTO;  
  10. namespace AzureFunctionsUnitTesting {  
  11.     public class ContentProcessor {  
  12.         non-public readonly ICustomerRepository _customerRepository;  
  13.         public ContentProcessor(ICustomerRepository customerRepository) {  
  14.                 _customerRepository = customerRepository;  
  15.             }  
  16.             [FunctionName(“ContentProcessor”)]  
  17.         public async Process Run([ServiceBusTrigger(“contentmessage”, Connection = “ServiceBusConnectionString”)] Message message, MessageReceiver messageReceiver) {  
  18.             attempt {  
  19.                 var messageBody = Encoding.UTF8.GetString(message.Physique);  
  20.                 var content material = JsonConvert.DeserializeObject < CustomerDto > (messageBody);  
  21.                 await _customerRepository.InsertAsync(content material);  
  22.             } catch (Exception ex) {}  
  23.         }  
  24.     }  
  25. }  

You must present the queue identify and repair bus connection string as parameters for the perform. On this perform, I obtained the message from queue which is a Json object and I’ve deserialized into my Dto mannequin and making an attempt to insert it into my Db.

 

So I’ve my perform right here, however how do I do know whether or not my perform will work once I publish this perform? To ensure that the perform has no defects in it, we can be making a unit take a look at technique for Azure features. Additionally, most significantly you probably have your unit assessments coated, any breaking change in our system might be detected by operating our take a look at strategies. Once we begin creating by take a look at pushed improvement, it is going to enhance the boldness of a developer on his code.

 

Unit Take a look at for Azure Perform

 

At first, I can be making a base class for my assessments the place I can be mocking my Db Context, ServiceBus connection string and so on which can be used widespread throughout my assessments.

  1. utilizing Autofac;  
  2. utilizing Area;  
  3. utilizing Microsoft.Azure.ServiceBus;  
  4. utilizing Microsoft.Azure.ServiceBus.Core;  
  5. utilizing Microsoft.EntityFrameworkCore;  
  6. utilizing Microsoft.EntityFrameworkCore.Diagnostics;  
  7. utilizing System;  
  8. utilizing System.IO;  
  9. utilizing System.Textual content;  
  10. namespace AzureFunctionsUnitTesting {  
  11.     public class TestsBase {  
  12.         public DevelopmentDbContext _context;  
  13.         protected MessageReceiver _messageReceiver;  
  14.         public TestsBase() {  
  15.             var choices = new DbContextOptionsBuilder < DevelopmentDbContext > ().UseInMemoryDatabase(databaseName: $ “DevelopmentDb{Guid.NewGuid()}”).ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning)).Choices;  
  16.             _context = new DevelopmentDbContext(choices);  
  17.             var containerBuilder = new ContainerBuilder();  
  18.             var cn = “Endpoint=sb://commoncomponentsqaus.servicebus.home windows.web/;SharedAccessKeyName=ClientKey;SharedAccessKey=————–“;  
  19.             var connectionString = new ServiceBusConnectionStringBuilder(cn);  
  20.             connectionString.EntityPath = “Take a look at”;  
  21.             _messageReceiver = new MessageReceiver(connectionString);  
  22.         }  
  23.         inside Message LoadMessage(string fileName) {  
  24.             attempt {  
  25.                 var path = AppDomain.CurrentDomain.BaseDirectory + “Json” + fileName;  
  26.                 var json = File.ReadAllText(path);  
  27.                 var bytes = Encoding.UTF8.GetBytes(json);  
  28.                 var message = new Message(bytes);  
  29.                 return message;  
  30.             } catch (Exception ex) {  
  31.                 string g = ex.Message;  
  32.                 var message = new Message();  
  33.                 return message;  
  34.             }  
  35.         }  
  36.     }  
  37. }  

Now I can be writing my unit take a look at technique by inheriting the Testsbase class.

 

Under is piece of code for my Unit take a look at of Azure perform. I’ve a mock Json Object(CustomerMessage.Json)

  1. {  
  2.     “id”: 1,  
  3.     “CustomerName”“Take a look at Consumer”,  
  4.     “Nation”“India”,  
  5.     “JoiningDate”“28-09-2020”,  
  6.     “PrimeUser”false  
  7. }  

I’ll initializing an object of my perform and I’ve a mock db context. I’ll sending the Json object within the type of a message. I’ll run the perform and the perform will course of the message Object I’ve despatched. As our perform is to verify the data obtained inserted or not, I’ll verify the rely of data in area I’ve mocked up.

  1. utilizing Area.Repositories;  
  2. utilizing Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. utilizing System.Linq;  
  4. utilizing System.Threading.Duties;  
  5. namespace AzureFunctionsUnitTesting {  
  6.     [TestClass]  
  7.     public class ContentProcessorTests: TestsBase {  
  8.         [TestMethod]  
  9.         public async Process Insert_Countries_Into_Db() {  
  10.               
  11.             var message = LoadMessage(“CustomerMessage.json”);  
  12.             var customerRepository = new CustomerRepository(_context);  
  13.             var  
  14.             perform = new ContentProcessor(customerRepository);  
  15.               
  16.             Assert.AreEqual(0, _context.Clients.Rely());  
  17.               
  18.             await  
  19.             perform.Run(message, _messageReceiver);  
  20.               
  21.               
  22.             var dbCount = _context.Clients.Rely();  
  23.             Assert.AreEqual(1, dbCount);  
  24.         }  
  25.     }  
  26. }  

 I hope this text helps in writing unit assessments on your Azure features.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button