A
A
Andrey PINEAPPLE2016-11-04 21:51:59
C++ / C#
Andrey PINEAPPLE, 2016-11-04 21:51:59

What does the code (NSubstitute, NUnit) do?

There is an example of using NSubstitute for stub/mock testing.
In this example, a WFA is created :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BankServiceApp.UI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public event EventHandler<ConnectionArgs> OnValidate = delegate { };
        public bool IsValidationOk { get; set; }

        private void Validate(ConnectionArgs args)
        {
            if (OnValidate != null)
                MessageBox.Show("Validation OK");
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Validate(new ConnectionArgs {Login = "User", Password = "User"});
        }
    }

    public interface IConnectValidation
    {
        event EventHandler<ConnectionArgs> OnValidate;
        bool IsValidationOk { get; }
    }

    public class ConnectionArgs : EventArgs
    {
        public string Login { get; set; }
        public string Password { get; set; }
    }
}

and Unit Test Project :
using System;
using System.Runtime.Remoting.Channels;
using NUnit.Framework;
using NSubstitute;
using BankServiceApp.UI;

namespace BankServiceAppTests
{
    [TestFixture]
    public class UITests
    {
        [Test]
        public void OnValidate_WasCalled_ReturnsTrue()
        {
            // arrange
            var wasCalled = false;
            var validator = Substitute.For<IConnectValidation>();
            validator.OnValidate += (sender, args) => wasCalled = true;

            // act
            validator.OnValidate += Raise.EventWith(new ConnectionArgs());

            // assert
            Assert.NotNull(validator);
            Assert.IsTrue(wasCalled);
        }
    }
}

In the links of which the BankServiceApp.UI project is added, frameworks: NUnit and NSubstitute.
Actually the code itself works fine and the test passes successfully. It's just that I don't quite understand what's going on.
This is where your help is needed. Comment out the code in as much detail as possible.
Particularly interested in:
event EventHandler<ConnectionArgs> OnValidate;
public event EventHandler<ConnectionArgs> OnValidate = delegate { };

var validator = Substitute.For<IConnectValidation>();
            validator.OnValidate += (sender, args) => wasCalled = true;

            // act
            validator.OnValidate += Raise.EventWith(new ConnectionArgs());

The general logic in the main program is also of interest (that is, what and why is happening there at all).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-11-05
@Got_Oxidus

A simple class for testing.

using System;
using System.Runtime.Remoting.Channels;
using NUnit.Framework;
using NSubstitute;
using BankServiceApp.UI;

namespace BankServiceAppTests
{
    [TestFixture]
    public class UITests
    {
        [Test] //метод-тестировщик.
        public void OnValidate_WasCalled_ReturnsTrue()
        {
            // arrange 
            var wasCalled = false;
            var validator = Substitute.For<IConnectValidation>();
            validator.OnValidate += (sender, args) => wasCalled = true;

            // act
            validator.OnValidate += Raise.EventWith(new ConnectionArgs());

            // assert
            Assert.NotNull(validator); // Проверка на (validator != null)
            Assert.IsTrue(wasCalled); // Проверка на (wasCalled == true)
//если эти два условия верны, то тест пройден, если нет, то не пройден.
        }
    }
}

event EventHandler<ConnectionArgs> OnValidate;//создание события
public event EventHandler<ConnectionArgs> OnValidate = delegate { }; //Защита от NullReferenceException. Присвоив событию пустой делегат.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question