Answer the question
In order to leave comments, you need to log in
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; }
}
}
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);
}
}
}
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());
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question