Rhino Mock and Mock in general

Is there a place or someone who can explain how this works in plain English, rather than “ defining terms in terms of ourselves ”?

+3
source share
4 answers

So, you have a class that depends on something else.

Metaphor used: an automobile is required for a car.

The car depends on the engine. It is easy to verify that the car and engine are working together, but what about testing a car without an engine, or that the car is “correctly” calling the engine. We can do something (false) instead of the engine and press the gas (make a call) and make sure that the fake (mock) engine got the correct entry into the throttle body. Instead of checking the entire system, you just tested the item you want to isolate by measuring with a mock object.

It becomes more complex and more effective in practice, but ...

+9
source

, - , , . , . , , , .

, . . , -, , .

Mocking frameworks, Rhino Mock, , , . , , , .

+3

Mocks, .

(, rhino mocks)

,

- , (, ) ( ) , . , - , , . 2 (IStringAnalyzer ILooger):

class SomeClass
{
    IStringAnalyzer stringAnalizer;
    ILogger logger;

    public SomeClass(IStringAnalyzer stringAnalyzer, ILogger logger)
    {   
        this.logger = logger;
        this.stringAnalyzer = stringAnalyzer;
    }


    public void SomeMethod(string someParameter)
    {
        if (stringAnalyzer.IsValid(someParameter))
        {
            logger.Log("Invalid string");
        }else
        {
            //do something with someParameter
        }
    }
}

, SomeClass SomeMethod , ILogger " ". "" IStringAnalyzer ILogger, , , , , , . , , , - , .

: IStringAnalyzer ILogger, . , . IStringAnalyzer , , , false, , . (someParameter).

ILogger , , "Invalid string", .

IStringAnalyzer ILogger "" ( ), - Stub (IStringAnalyzer), - (ILogger). , , ( IStringAnalyzer IsValid false). , ( ILogger). ( ) ( , ). , ( ). .

"", :

class StringAnalyzerStub : IStringAnalyzer 
{   
    public bool whatToReturn;

    public StubStringAnalyzerStub(bool whatToReturn)
    {
        this.whatToReturn = whatToReturn;
    }

    public bool IsValid(string parameter)
    {
        return whatToReturn;
    }
}


class LoggerMock : ILogger
{
    public string WhatWasPassedIn;

    public void Log(string message)
    {
        WhatWasPassedIn = message;
    }
}

:

[Test]
public void SomeMethod_InvalidParameter_CallsLogger
{
    IStringAnalyzer s = new StringAnalyzerStub(false); //will always return false when IsValid is called
    ILogger l = new LoggerMock();
    SomeClass someClass = new SomeClass(s, l);

    someClass.SomeMethod("What you put here doesnt really matter because the stub will always return false");

    Assert.AreEqual(l.WhatWasPassedIn, "Invalid string");
}

, , , , , , Rhino Mocks. mocks stub, Rhino Mocks ( arr, act, assert):

[Test]
public void SomeMethod_InvalidParameter_CallsLogger
{
    Rhino.Mocks.MockRepository mockRepository = new Rhino.Mocks.MockRepository();
    IStringAnalyzer s = mockRepository.Stub<IStringRepository>();
    s.Expect(s => s.IsValid("something, doesnt matter").IgnoreParameters().Return(false);
    ILogger l = mockRepository.DynamicMock<ILogger>();
    l.Log("Invalid string");
    SomeClass someClass = new SomeClass(s, l);
    mockRepository.ReplayAll();

    someClass.SomeMethod("What you put here doesnt really matter because the stub will always return false");

    l.AssertWasCalled(l => l.Log("Invalid string"));
}

, :)

: , ...

+2

- , . , , .

Unlike stamps, mocks keeps track of actual usage, so your test can confirm that the layout was used as expected.

See the Fowler article here for more information .

+1
source

Source: https://habr.com/ru/post/1704171/


All Articles