Adding partial substitute examples
This commit is contained in:
Родитель
abee0831d7
Коммит
526e91d7f1
|
@ -75,6 +75,7 @@
|
|||
<Compile Include="FieldReports\CallingIntoNewSubWithinReturns.cs" />
|
||||
<Compile Include="FieldReports\Issue110_CustomExceptions.cs" />
|
||||
<Compile Include="FieldReports\Issue_ArgumentCheckOfOptionalParameter.cs" />
|
||||
<Compile Include="PartialSubExamples.cs" />
|
||||
<Compile Include="ReturnsAndDoes.cs" />
|
||||
<Compile Include="ExceptionsWhenCheckingReceivedCalls.cs" />
|
||||
<Compile Include="ExceptionsWhenCheckingSequencesOfCalls.cs" />
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NSubstitute.Acceptance.Specs.Infrastructure;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace NSubstitute.Acceptance.Specs
|
||||
{
|
||||
public class PartialSubExamples
|
||||
{
|
||||
public class TemplateMethod
|
||||
{
|
||||
public class FormResult
|
||||
{
|
||||
public bool IsValid { get; set; }
|
||||
public bool IsComplete { get; set; }
|
||||
}
|
||||
|
||||
public abstract class Form
|
||||
{
|
||||
public virtual FormResult Submit()
|
||||
{
|
||||
var result = Validate();
|
||||
if (result.IsValid)
|
||||
{
|
||||
Save();
|
||||
result.IsComplete = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract FormResult Validate();
|
||||
public abstract void Save();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidFormSubmission()
|
||||
{
|
||||
var form = Substitute.For<Form>();
|
||||
form.Submit().Returns(x => x.CallBase());
|
||||
form.Validate().Returns(new FormResult() { IsValid = true });
|
||||
|
||||
var result = form.Submit();
|
||||
|
||||
Assert.That(result.IsComplete);
|
||||
form.Received().Save();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidFormSubmission()
|
||||
{
|
||||
var form = Substitute.For<Form>();
|
||||
form.Submit().Returns(x => x.CallBase());
|
||||
form.Validate().Returns(new FormResult() { IsValid = false });
|
||||
|
||||
var result = form.Submit();
|
||||
|
||||
Assert.That(result.IsComplete, Is.False);
|
||||
form.DidNotReceive().Save();
|
||||
}
|
||||
}
|
||||
|
||||
public class TemplateMethod2Example
|
||||
{
|
||||
public class SummingReader
|
||||
{
|
||||
public virtual int Read()
|
||||
{
|
||||
var s = ReadFile();
|
||||
return s.Split(',').Select(int.Parse).Sum();
|
||||
}
|
||||
public virtual string ReadFile() { return "the result of reading the file here"; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldSumAllNumbersInFile()
|
||||
{
|
||||
var reader = Substitute.For<SummingReader>();
|
||||
reader.Read().Returns(x => x.CallBase());
|
||||
reader.ReadFile().Returns("1,2,3,4,5");
|
||||
|
||||
var result = reader.Read();
|
||||
|
||||
Assert.That(result, Is.EqualTo(15));
|
||||
}
|
||||
}
|
||||
|
||||
public class UnderlyingListExample
|
||||
{
|
||||
public class TaskList
|
||||
{
|
||||
readonly List<string> list = new List<string>();
|
||||
public virtual void Add(string s) { list.Add(s); }
|
||||
public virtual string[] ToArray() { return list.ToArray(); }
|
||||
}
|
||||
|
||||
public class TaskView
|
||||
{
|
||||
private readonly TaskList _tasks;
|
||||
public string TaskEntryField { get; set; }
|
||||
public string[] DisplayedTasks { get; set; }
|
||||
|
||||
public TaskView(TaskList tasks) { _tasks = tasks; }
|
||||
|
||||
public void ClickButton()
|
||||
{
|
||||
_tasks.Add(TaskEntryField);
|
||||
DisplayedTasks = _tasks.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddTask()
|
||||
{
|
||||
var list = Substitute.For<TaskList>();
|
||||
// TODO: Substitute.OnDemandFor<TaskList>() (call base by default, override to opt out. Handling voids?)
|
||||
list.WhenForAnyArgs(x => x.Add("")).Do(x => x.CallBase());
|
||||
list.ToArray().Returns(x => x.CallBase());
|
||||
|
||||
var view = new TaskView(list);
|
||||
view.TaskEntryField = "write example";
|
||||
|
||||
view.ClickButton();
|
||||
|
||||
// list substitute functions as test spy
|
||||
list.Received().Add("write example");
|
||||
Assert.That(view.DisplayedTasks, Is.EqualTo(new[] { "write example" }));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче