Rp
is a finite set of essential data of the programming environment.f(x) : Rp -> Rp
is the name of a certain code sequence executed on the data x
from Rp
.f(x)
is a simple class method that takes x
as input. If it is even tougher, then f
is just a line of code).z = h(x)
, where h
is a test function. Fix some value x o
, then z o = h(x o )
. Now we define the function a(z o )
, which returns 0 (if z o
incorrect) or 1 (if z o
correct). In other words, we took some test data x o
, made some manipulations with them in the form of h(x o )
and got z o
. Then we made an assert
for the received data z o
and checked the validity of the test.def x o
z o = h(x o )
a(z o )
f(x)
be a functional code (one that will work in the entity being developed).z = h(x) = f(m(x))
, where m(x)
is an auxiliary code: stub objects of the functional code dependencies, framework framework structures, etc. (further m(x)
- moki)m(x) = x
. Hence the separation f(m(x)) = f(x)
. The latter allows you to clearly describe the test creation algorithm, where the functional code is developed together with the test code.def x o
def z o
a(z o )
def x o
z o = f(x o )
a(z o )
def x o
z o = f(m(x o ))
a(z o )
f(x)
into the entity being developeddef x o
z o = h(x o )
a(z o )
/// <summary>
///
/// </summary>
/// <remarks>
/// ,
/// </remarks>
public interface INodeResolver
{
/// <summary>
///
/// </summary>
/// <param name="id"> </param>
/// <returns></returns>
Node FindById( int id);
}
* This source code was highlighted with Source Code Highlighter .
ITopologyService
is responsible for these topologies:/// <summary>
///
/// </summary>
public interface ITopologyService
{
/// <summary>
/// "" (, , , , etc)
/// </summary>
DataSets.TopologyData GetTopology(IDataFilter filter);
}
* This source code was highlighted with Source Code Highlighter .
ITopologyService
, receives data from it and creates a new instance of the Node
class using the passed identifier.[Test]
public void FindNodeByIdTest()
{
// x0
TopologyData data = new TopologyData();
data.Node.AddNodeRow(1, "1" , Guid .NewGuid());
// z0
Node node = new Node { Id = 1, Name = "1" };
Assert.AreEqual(1, node.Id);
Assert.AreEqual( "1" , node.Name);
}
* This source code was highlighted with Source Code Highlighter .
[Test]
public void FindNodeByIdTest2()
{
// x0
TopologyData data = new TopologyData();
data.Node.AddNodeRow(1, "1" , Guid .NewGuid());
// f(x0)
TopologyData.NodeRow nodeRow = data.Node.FindByID(1);
// z0
Node node = new Node { Id = 1, Name = nodeRow.Description };
Assert.AreEqual(1, node.Id);
Assert.AreEqual( "1" , node.Name);
}
* This source code was highlighted with Source Code Highlighter .
[Test]
public void FindNodeByIdTest3()
{
MockRepository repo = new MockRepository();
// x0
TopologyData data = new TopologyData();
data.Node.AddNodeRow(1, "1" , Guid .NewGuid());
// m(x0)
ITopologyService service = repo.StrictMock<ITopologyService>();
service.Expect(x => x.GetTopology(EmptyFilter.Instance)).Return(data).Repeat.Once();
repo.ReplayAll();
// f(m(x0)) = f(x0)
TopologyData dataSet = service.GetTopology(EmptyFilter.Instance);
TopologyData.NodeRow nodeRow = dataSet.Node.FindByID(1);
repo.VerifyAll();
// z0
Node node = new Node { Id = 1, Name = nodeRow.Description };
Assert.AreEqual(1, node.Id);
Assert.AreEqual( "1" , node.Name);
}
* This source code was highlighted with Source Code Highlighter .
[Test]
public void FindNodeByIdTest4()
{
MockRepository repo = new MockRepository();
// x0
TopologyData data = new TopologyData();
data.Node.AddNodeRow(1, "1" , Guid .NewGuid());
// m(x0)
ITopologyService service = repo.StrictMock<ITopologyService>();
service.Expect(x => x.GetTopology(EmptyFilter.Instance)).Return(data).Repeat.Once();
repo.ReplayAll();
NodeResolver resolver = new NodeResolver(service);
// z0
Node node = resolver.FindById(1);
repo.VerifyAll();
Assert.AreEqual(1, node.Id);
Assert.AreEqual( "1" , node.Name);
}
* This source code was highlighted with Source Code Highlighter .
NodeResolver
turned out like this:/// <summary>
///
/// </summary>
public class NodeResolver : INodeResolver
{
public NodeResolver(ITopologyService topologyService)
{
Guard.ArgumentNotNull(topologyService, "service" );
_data = topologyService.GetTopology(EmptyFilter.Instance);
}
#region INodeResolver Members
public Node FindById( int id)
{
// f(x)
return new Node { Id = id, Name = _data.Node.FindByID(id).Description };
}
#endregion
private TopologyData _data;
}
* This source code was highlighted with Source Code Highlighter .
f(x)
is implemented) is the time saving and “smearing” of the development. The programmer no longer has to spend time on code that does not directly relate to the functionality of the program. He writes the code together with the test, making two rabbits one (the refactoring itself is o-small, which can be discarded).Source: https://habr.com/ru/post/49434/
All Articles