Replaced Moq with Nsubstitute

This commit is contained in:
abdelkader
2020-12-28 09:06:45 -05:00
parent 3f66b15e0b
commit b3cee3a9d2
6 changed files with 201 additions and 129 deletions

View File

@@ -0,0 +1,26 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using vCardEditor.Repository;
using VCFEditor.Repository;
namespace vCardEditor_Test
{
[TestClass]
public class ContactRepositoryTest
{
[TestMethod]
public void NewFileOpened_Utf8Entry_Test()
{
var fileHandler = Substitute.For<IFileHandler>();
fileHandler.ReadAllLines(Arg.Any<string>()).Returns(Entries.vcfUtf8Entry);
var repo = Substitute.For<ContactRepository>(fileHandler);
Assert.AreEqual(repo.LoadContacts("name")[0].Name, "Oum Alaâ");
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vCardEditor_Test
{
public class Entries
{
public static string[] vcfOneEntry
{
get
{
string s = @"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"LABEL;QUOTED-PRINTABLE;WORK;PREF:Rue Th. Decuyper 6A=Bruxelles 1200=Belgique\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD";
return s.Split('\n');
}
}
public static string[] vcfThreeEntry
{
get
{
string s = "BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD\n" +
"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD\n" +
"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont3\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD";
return s.Split('\n');
}
}
public static string[] vcfUtf8Entry
{
get
{
string s = "BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=41=6C=61=C3=A2;=4F=75=6D;;;\n" +
"FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=4F=75=6D=20=41=6C=61=C3=A2\n" +
"END:VCARD";
return s.Split('\n');
}
}
}
}

View File

@@ -11,108 +11,57 @@ using VCFEditor.Model;
using System.ComponentModel;
using VCFEditor.Repository;
using vCardEditor.Repository;
using NSubstitute;
namespace vCardEditor_Test
{
/// <summary>
///
/// </summary>
[TestClass]
public class MainPresenterTest
{
#region vCard Entries
public string[] vcfOneEntry
{
get
{
string s = @"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"LABEL;QUOTED-PRINTABLE;WORK;PREF:Rue Th. Decuyper 6A=Bruxelles 1200=Belgique\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD";
return s.Split('\n');
}
}
public string[] vcfThreeEntry
{
get
{
string s = "BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD\n" +
"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont1\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD\n" +
"BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"FN:Jean Dupont3\n" +
"N:Dupont;Jean\n" +
"ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper\n" +
"TEL;CELL:+1234 56789\n" +
"EMAIL;INTERNET:jean.dupont@example.com\n" +
"END:VCARD";
return s.Split('\n');
}
}
#endregion
[TestMethod]
public void NewFileOpenedTest()
{
var handler = new Mock<IFileHandler>();
var repo = new Mock<ContactRepository>(handler.Object);
var view = new Mock<IMainView>();
handler.Setup(x => x.ReadAllLines("filename"))
.Returns(vcfOneEntry);
var fileHandler = Substitute.For<IFileHandler>();
fileHandler.ReadAllLines(Arg.Any<string>()).Returns(Entries.vcfOneEntry);
var repo = Substitute.For<ContactRepository>(fileHandler);
var view = Substitute.For<IMainView>();
var presenter = new MainPresenter(view, repo);
view.NewFileOpened += Raise.EventWith(new EventArg<string>("aaa"));
var presenter = new MainPresenter(view.Object, repo.Object);
view.Raise(m => m.NewFileOpened += null, new EventArg<string>("filename"));
view.Received().DisplayContacts(Arg.Is<BindingList<Contact>>(x=>x.Count == 1));
view.Received().DisplayContacts(Arg.Is<BindingList<Contact>>(x => x[0].card.FormattedName == "Jean Dupont1"));
view.Verify(m => m.DisplayContacts(It.Is<BindingList<Contact>>(x => x.Count == 1)));
view.Verify(m => m.DisplayContacts(It.Is<BindingList<Contact>>(x => x[0].card.FormattedName == "Jean Dupont1")));
}
[TestMethod]
public void DeleteTest()
{
var handler = new Mock<IFileHandler>();
var repo = new Mock<ContactRepository>(handler.Object);
var view = new Mock<IMainView>();
handler.Setup(x => x.ReadAllLines("filename"))
.Returns(vcfThreeEntry);
var presenter = new MainPresenter(view.Object, repo.Object);
view.Raise(m => m.NewFileOpened += null, new EventArg<string>("filename"));
var fileHandler = Substitute.For<IFileHandler>();
fileHandler.ReadAllLines(Arg.Any<string>()).Returns(Entries.vcfThreeEntry);
var repo = Substitute.For<ContactRepository>(fileHandler);
var view = Substitute.For<IMainView>();
var presenter = new MainPresenter(view, repo);
view.NewFileOpened += Raise.EventWith(new EventArg<string>("aaa"));
//Mouse click on second row.
repo.Object.Contacts[1].isSelected = true;
repo.Contacts[1].isSelected = true;
//Delete the second one.
view.Raise(m => m.DeleteContact += null, null, null);
Assert.AreEqual(repo.Object.Contacts.Count, 2);
Assert.AreEqual(repo.Object.Contacts[1].card.FormattedName, "Jean Dupont3");
view.DeleteContact += Raise.Event();
Assert.AreEqual(repo.Contacts.Count, 2);
Assert.AreEqual(repo.Contacts[1].card.FormattedName, "Jean Dupont3");
}
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.2.0" targetFramework="net45" />
<package id="NSubstitute" version="3.1.0" targetFramework="net45" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net45" />
</packages>

View File

@@ -36,11 +36,21 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.2.0\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Moq">
<HintPath>Lib\Moq.dll</HintPath>
</Reference>
<Reference Include="NSubstitute, Version=3.1.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<HintPath>..\packages\NSubstitute.3.1.0\lib\net45\NSubstitute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
@@ -48,6 +58,10 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
@@ -58,6 +72,8 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="ContactRepositoryTest.cs" />
<Compile Include="Entries.cs" />
<Compile Include="FixedListTest.cs" />
<Compile Include="MainPresenterTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -71,6 +87,9 @@
<ItemGroup>
<Folder Include="Lib\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.