Basit Bir Mock Framework Geliştirelim 2

2023-05-11

Artık interface lerle çalışalım.

Öncelikle, System.Reflection.DispatchProxy‘yi kullanacağız. Bu, bir proxy sınıfı oluşturmanıza ve ona method çağrılarını yönlendirmenize olanak sağlar.

DispatchProxy‘yi kullanarak belirli bir arayüzün metotlarını mocklayalım.

using System;
using System.Collections.Generic;
using System.Reflection;

public class Mock<T> : DispatchProxy
{
    private readonly Dictionary<string, object> _methodResponses = new();

    protected override object Invoke(MethodInfo targetMethod, object[] args)
    {
        if (_methodResponses.TryGetValue(targetMethod.Name, out var value))
        {
            return value;
        }

        throw new Exception($"No response set up for method {targetMethod.Name}");
    }

    public void Setup(string methodName, object response)
    {
        if (_methodResponses.ContainsKey(methodName))
        {
            _methodResponses[methodName] = response;
        }
        else
        {
            _methodResponses.Add(methodName, response);
        }
    }

    public static T Create()
    {
        return Create<T, Mock<T>>();
    }
}

Bu örnekte, Setup metodu belirli bir methodun yanıtını ayarlar. Daha sonra, Invoke metodu çağrıldığında, belirli bir method için ayarlanan yanıtı döndürür.

Bunu aşağıdaki gibi kullanabiliriz:

public interface ITestInterface
{
    string TestMethod();
}

public class Test
{
    public void TestMethod()
    {
        var mock = Mock<ITestInterface>.Create();
        (mock as Mock<ITestInterface>).Setup(nameof(ITestInterface.TestMethod), "Test response");

        var result = mock.TestMethod();

        Console.WriteLine(result); // Outputs: Test response
    }
}

Bu örnekte, ITestInterface arayüzünün TestMethod metodunu mockluyoruz. TestMethod çağrıldığında, “Test response” döndürülür.

Bu basit mocklama kütüphanesi, belirli bir arayüzün metotlarını mocklamak için kullanılabilir.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 