// 將座標丟到新分裂出來的四個四元樹象限,符合座標坐落的位置,存放好回傳True if (Insert(node.NorthWest, data)) returntrue; if (Insert(node.NorthEast, data)) returntrue; if (Insert(node.SouthWest, data)) returntrue; if (Insert(node.SouthEast, data)) returntrue;
using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using WebApplication4.Models; using System.IO; using CsvHelper; using System.Linq; using System.Reflection; using System.Linq; namespaceWebApplication4.Tests { [TestClass] publicclassEmployeesRepositoryTests {
[TestInitialize] publicvoidInitial() { //讀取檔案 using (StreamReader reader = new StreamReader( string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"\CSVs\Employees.csv"), new UTF8Encoding())) using (var csvReader = new CsvReader(reader)) { csvReader.Configuration.WillThrowOnMissingField = false; var Employees = csvReader.GetRecords<Employees>().ToList();
//將資料寫入DB var DBContext = new NorthwindEntities(); DBContext.Employees.AddRange(Employees); DBContext.SaveChanges(); } }
[TestMethod] publicvoid Get_帶入ID_應取回該ID的Employee() { //arrange var ID = 0; var Sut = new EmployeesRepository();
var Expected = "Nancy"; //act var actual = Sut.Get(ID);
using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using WebApplication4.Models; using System.IO; using CsvHelper; using System.Linq; using System.Reflection; using System.Data.SqlClient; using Dapper; namespaceWebApplication4.Tests { [TestClass] publicclassEmployeesRepositoryTests {
[TestInitialize] publicvoidInitial() { //讀取檔案 using (StreamReader reader = new StreamReader( string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"\CSVs\Employees.csv"), new UTF8Encoding())) using (var csvReader = new CsvReader(reader)) { csvReader.Configuration.WillThrowOnMissingField = false; var Employees = csvReader.GetRecords<Employees>().ToList();
//將資料寫入DB //var DBContext = new NorthwindEntities(); //DBContext.Employees.AddRange(Employees); //DBContext.SaveChanges(); using (var cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindString"].ConnectionString)) { cn.Execute(@"SET IDENTITY_INSERT Employees ON INSERT INTO Employees (EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Notes,ReportsTo,PhotoPath) VALUES (@EmployeeID,@LastName,@FirstName,@Title,@TitleOfCourtesy,@BirthDate,@HireDate,@Address,@City,@Region,@PostalCode,@Country,@HomePhone,@Extension,@Notes,@ReportsTo,@PhotoPath) SET IDENTITY_INSERT Employees OFF", Employees); }
} }
[TestMethod] publicvoid Get_帶入ID_應取回該ID的Employee() { //arrange var ID = 1; var Sut = new EmployeesRepository();
var Expected = "Nancy"; //act var actual = Sut.Get(ID);
```csharp void Main() { //修改匯出CSV資料存放的位置 using (var sw = new StreamWriter(@"D:\Employees.csv")) using (var writer = new CsvWriter(sw)) { var result = new List<Employees>(); var connectionString = this.Connection.ConnectionString;
using (var conn = new SqlConnection(connectionString)) { conn.Open();
var sqlCommand = new StringBuilder(); //要匯出哪些資料的SQL SCript sqlCommand.AppendLine(@"select top 10 * from Employees"); result = conn.Query<Employees>(sqlCommand.ToString()) .ToList(); } writer.WriteRecords(result); } } //將第一支程式產出的Class貼在這邊!!!
public class Employees { public int EmployeeID { get; set; }
之前有寫篇文章[【擴充WebConfig】](http://toyo0103.blogspot.tw/2013/09/webconfigwebconfig.html)有提到如何在專案中加掛Config檔案,讓設定檔能做簡單的歸類整理。原本以為Visual Studio應該會有相關的功能,可以對這類自訂的Config分割出對應版本,結果東找西找都找不到方法去做對應,原來VS似乎沒有提供這個功能(?)還好在網路上找到了解決的套件跟方法[How to add config transformations for a custom config file in Visual Studio?](http://stackoverflow.com/questions/34735132/how-to-add-config-transformations-for-a-custom-config-file-in-visual-studio)
```csharp public class UnitTestSampleBase { public UnitTestSampleBase() { }
protected virtual int GetValue() { //開一個virtual的Method,將外部引用隔離出來 ThirdPartyObject tpobj = new ThirdPartyObject(); return tpobj.GetValue(); }
public int WantUnitTestMethod() { //避免直接引用 int value = GetValue();
return value + 1; } }
做一個Fake的受測對象,並繼承受測得目標,並改寫取得外部資源的結果的Method,讓它回傳我們寫死的固定值 ```csharp public class UnitTestSampleFake : UnitTestSampleBase {
protected override int GetValue()
{
return 1;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
3. 進行測試 ```csharp [TestClass] public class UnitTestSampleFakeTest { [TestMethod] public void WantUnitTestMethod_Call_ShouldRetuen2() { //arrange var sut = new UnitTestSampleFake(); //act var actual = sut.WantUnitTestMethod(); //assert //1+1 = 2 Assert.AreEqual(actual, 2); } }