Basit Bir Mock Framework Geliştirelim 1

2023-05-11

Mocklama, testlerinizde belirli sınıfların veya metodların davranışlarını taklit etmek için kullanılır. Örneğin, bir veritabanı çağrısı yapmak yerine, bir mock veritabanı çağrısını simüle edebilirsiniz. Bu, testlerin daha hızlı çalışmasını sağlar ve testlerin bağımlılıklarını azaltır.

.NET’de birçok popüler mock kütüphanesi varken neden böyle bir şey yapalım ki. Sadece nasıl çalıştığını anlamak ve biraz pratik yapmak. Ve başlayalım

using System;
using System.Collections.Generic;

public interface IMock<T>
{
    void When(Func<T, bool> predicate);
    void ThenReturn(object response);
}

public class Mock<T> : IMock<T>
{
    private Dictionary<Func<T, bool>, object> responses = new();

    public void When(Func<T, bool> predicate)
    {
        if (!responses.ContainsKey(predicate))
        {
            responses.Add(predicate, null);
        }
    }

    public void ThenReturn(object response)
    {
        var lastKey = new List<Func<T, bool>>(responses.Keys)[^1];
        responses[lastKey] = response;
    }

    public object Handle(T input)
    {
        foreach (var pair in responses)
        {
            if (pair.Key(input))
            {
                return pair.Value;
            }
        }

        throw new Exception("No matching response for input");
    }
}

Bu basit mocklama kütüphanesi, belirli girdilere belirli yanıtlar verir. Örneğin, aşağıdaki gibi kullanılabilir:

var mock = new Mock<int>();
mock.When(x => x == 5).ThenReturn("Five");
mock.When(x => x == 10).ThenReturn("Ten");

Console.WriteLine(mock.Handle(5)); // Outputs: Five
Console.WriteLine(mock.Handle(10)); // Outputs: Ten

Bu örnekte, mock nesnesi 5 için “Five” ve 10 için “Ten” döndürür. Bu, belirli bir sınıfın veya metodun davranışını taklit etmek 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 