NSubstitute & Mock Implementasyonu

July 17, 2017

Hali hazırda yürütmekte olduğumuz projenin testlerini yazarken polonyalı meslektaşım mocking için NSubstitute kullandıklarını söyledi. Hali hazırda çok ilerlememiş olduğumuz için projenin testlerini Nsubstitute’ e taşıdık. Başlarda biraz soğuk gelse de zamanla ısındım ve doğal dile daha yakın ifadeler içerdiğini gördüm. Kötü yanı dokümantasyonu çok açıklayıcı değil ancak çok fazla ihtiyaç duymuyorsunuz. Strategy pattern/Proxy implementasyonunu nasıl test edeceğimize dair küçük bir örnek yaptım.

Fikir vermesi için testleri burada paylaşıyorum.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class CommunicationTests
{
    [Test]
    public void PassMessageWithoutModify()
    {
        var message = new Message() { };
        var result = new MessageResult() { StatusCode = StatusCode.OK };

        var mockSuccessListener = Substitute.For<IListener<MessageResult>>();
        var mockFailureListener = Substitute.For<IListener<MessageResult>>();
        var mockSender = Substitute.For<ISendingStrategy>();
        mockSender.Send(message).Returns(result);

        var target = new Context(mockSender, mockSuccessListener, mockFailureListener);
        target.Process(message);

        mockSender.Received(1).Send(message);
    }
    [Test]
    public void RedirectToSuccessListener()
    {
        var message = new Message() { };
        var result = new MessageResult() { StatusCode = StatusCode.OK };

        var mockSuccessListener = Substitute.For<IListener<MessageResult>>();
        var mockFailureListener = Substitute.For<IListener<MessageResult>>();
        var mockSender = Substitute.For<ISendingStrategy>();
        mockSender.Send(message).Returns(result);

        var target = new Context(mockSender, mockSuccessListener, mockFailureListener);
        target.Process(message);

        mockSuccessListener.Received(1).Handle(result);
    }
    [Test]
    public void RedirectToFailureListener()
    {
        var message = new Message() { };
        var result = new MessageResult() { StatusCode = StatusCode.TIMEOUT };

        var mockSuccessListener = Substitute.For<IListener<MessageResult>>();
        var mockFailureListener = Substitute.For<IListener<MessageResult>>();
        var mockSender = Substitute.For<ISendingStrategy>();
        mockSender.Send(message).Returns(result);

        var target = new Context(mockSender, mockSuccessListener, mockFailureListener);
        target.Process(message);

        mockFailureListener.Received(1).Handle(result);
    }
}

hidden

John von Neumann – The Man from the Future

Before I read The Man from the Future by Ananyo Bhattacharya, I only knew about John von Neumann in two contexts: that computers use the von Neumann architecture, and that he appeared in a story about a mathematical problem I … Continue reading →

via Henrik Warne's blog

The Review Is the Action Item

2024/05/30The Review Is the Action ItemI like to consider running an incident review to be its own action item. Other follow-ups emerging from it are a plus, but the point is to learn from incidents, and the review gives room for that to happen.This is no…

via Ferd.ca

HOWTO: Change your behavior

In theory, behavior change should be easy. At first glance, it seems like you control your behavior. So, if you desire different behavior, why doesn’t your behavior change as instantly as your desire to change it? In short, lasting change of habitual behavio…

via Matt Might's blog