ã¯ããã«
ããã¼ã¿ãã¼ã¹ãèªã¿æ¸ãããé¨åã®ã¦ããããã¹ããããã«ããã®ã«ã¯ãããã¤ãçç±ãããã¾ãã
- è¤æ°äººã§ãã¹ããåæã«å®è¡ããã¨ã競åãã
- ãã¼ã¿ãã¼ã¹ã使ã£ããã¹ãã¯ãæéãæãã
- ãã¼ã¿ãã¼ã¹å ã®ãã¼ã¿ãå¤ããã¨ããã¹ãã失æãã
ã1çªç®ã¯ãåèªã®éçºç°å¢ã«ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹ãç¨æãããã¨ã§ã解決ã§ãã¾ãã2çªç®ã®åé¡ã¯ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ããã³ã¼ãããã¸ãã¯ããåé¢ãã¦ããã¼ã¿ãã¼ã¹ã«å®éã«ã¢ã¯ã»ã¹ãããã¹ãã±ã¼ã¹ãæ¸ãããã¨ã§ãæ¹åã§ãã¾ãï¼ãã¸ãã¯ã®ãã¹ãã«ã¯ã¢ãã¯ãããã¼ã使ãã¾ãï¼ã3çªç®ã¯ããã¹ãã®ãã³ã«ãã¼ã¿ãã¼ã¹ã®å 容ãåæåãããã¨ãåºæ¬ã«ãªãã¾ãããããããã¨ãã¹ãã«é·ãæéãæããããã«ãªã£ã¦ãã¾ãã¾ãã
ãä»åã¯ããã¸ãã¹ãã¸ãã¯ã®éçºæã«ã¢ãã¯ãããã¼ã使ããããããã«ã¯ã©ãããããã¾ãããã¹ãæã«ãã¼ã¿ãã¼ã¹ã®å 容ãå®å®ãããã«ã¯ã©ãããããããããèãã¦ã¿ã¾ãã
対象èªè
- TDDã«èå³ããæã¡ã®.NET Frameworkã®éçºè ã
å¿ è¦ãªç°å¢
ããµã³ãã«ã³ã¼ãã試ãã¦ã¿ãã«ã¯ãC# 2010ï¼Expressã§å¯ï¼ã¾ãã¯Visual Studio 2012ï¼Expressã§å¯ï¼ãããã³NUnit 2.6ã¨SQL Server Compact 3.5ãå¿ è¦ã§ããæ¬ç¨¿å·çæç¹ã§ã¯ãä¸è¨ããå ¥æã§ãã¾ãã
-
C# 2010 Express: Microsoft Visual Studio Express
Visual Studio Express 2012: Visual Studio Express 2012 for Windows Desktop -
NUnit 2.6: NUnit V2 2.6.1
NUnitã®ã¤ã³ã¹ãã¼ã«æé : NUnit 2.5 ã®å°å ¥ Step by Stepï¼çè ãµã¤ããæ§ãã¼ã¸ã§ã³ã§ã®èª¬æã§ããåºæ¬çã«åãã§ãï¼ - SQL Server Compact 3.5: Microsoft SQL Server Compact 3.5 Service Pack 2
ãä»åã¯SQL Serverãå¿ è¦ã§ããç¡åã®ExpressãCompact 4.0çã§æ§ãã¾ããããNorthwindãã¼ã¿ãã¼ã¹ã使ãã¾ãããªãããµã³ãã«ã³ã¼ãã§ã¯SQL Server Compact 3.5 SP2ã使ã£ã¦ãã¾ãã®ã§ãæ¥ç¶æååãã¯ã©ã¹åãªã©ã¯é©å®èªã¿æ¿ãã¦ãã ããã
ã¨ããããã³ã¼ããä½ã£ã¦ã¿ã
ãããã§ã¯ãCustomersãã¼ãã«ããè¤æ°ä»¶ã®ãã¼ã¿ãåå¾ãã¦ããã³ã¼ãããã¨ããããæ¸ãã¦ã¿ã¾ããããä»åã¯ãADO.NETã使ããã¨ã«ãã¾ãã
ã製åã®ãã¸ãã¯ã®ä¸ã«ããããªã¡ã½ãããä½ãã¾ãã
ã¯ã©ã¹å.ã¡ã½ããå | 顧客管çãã¸ãã¯.åæ¹ä¸è´ã§å§åãæ¤ç´¢ãã()ã |
---|---|
å¼æ° | stringåã |
è¿å¤ | 顧客æ å ±ã¯ã©ã¹ã®ã³ã¬ã¯ã·ã§ã³åãCustomersãã¼ãã«ããã顧客ã®ãã¡ã¼ã¹ããã¼ã ã¾ãã¯ã©ã¹ããã¼ã ã¨å¼æ°ãåæ¹ä¸è´ã§æ¯è¼ãã¦ãä¸è´ãããã¼ã¿ãè¿ãã |
ãNothwindãã¼ã¿ãã¼ã¹ãåæç¶æ ã®ã¾ã¾ãªãã次ã®ãããªã¦ããããã¹ããæ¸ããã¨ãã§ãã¾ãã
[TestFixture] public class 顧客管çãã¸ãã¯Test { [TestCase] //製åã³ã¼ã:空ã®ãªã¹ããè¿ããCustomerã¯ã©ã¹ã¯å®£è¨ã ãã public void åæ¹ä¸è´ã§å§åãæ¤ç´¢ããTest_å¼æ°ã空ãªã0件() { var head = ""; IList<Customer> customers = 顧客管çãã¸ãã¯.åæ¹ä¸è´ã§å§åãæ¤ç´¢ãã(head); Assert.AreEqual(0, customers.Count()); } [TestCase] //製åã³ã¼ã:Customerã¯ã©ã¹ã¨SQLæ(ãã©ã¡ã¼ã¿ã¼æªä½¿ç¨)ãå®è£ public void åæ¹ä¸è´ã§å§åãæ¤ç´¢ããTest_ãã¡ã¼ã¹ããã¼ã ãä¸è´() { var head = "Th"; var customers = 顧客管çãã¸ãã¯.åæ¹ä¸è´ã§å§åãæ¤ç´¢ãã(head); var first = customers[0]; Assert.AreEqual("UK", first.Country); Assert.AreEqual("Around the Horn", first.CompanyName); Assert.AreEqual("AROUT", first.CustomerID); Assert.AreEqual("Thomas Hardy", first.ContactName); } [TestCase] //製åã³ã¼ã:SQLæãå®æããããã©ã¡ã¿ã©ã¤ãºãã¯ã¨ãªã¼ã«ä¿®æ£ public void åæ¹ä¸è´ã§å§åãæ¤ç´¢ããTest_ã©ã¹ããã¼ã ãä¸è´() { var head = "An"; var customers = 顧客管çãã¸ãã¯.åæ¹ä¸è´ã§å§åãæ¤ç´¢ãã(head); var first = customers[0]; Assert.AreEqual("Germany", first.Country); Assert.AreEqual("Alfreds Futterkiste", first.CompanyName); Assert.AreEqual("ALFKI", first.CustomerID); Assert.AreEqual("Maria Anders", first.ContactName); } }
ããã®ãã¹ããéãããã«ã製åã³ã¼ããæ¸ãã¾ãï¼å®éã¯ããã¹ãã±ã¼ã¹ã1ã¤ãã¤é²ãã¦ããã¾ãï¼ã
public class Customer { public string Country { get; set; } public string CompanyName { get; set; } public string CustomerID { get; set; } public string ContactName { get; set; } } public class 顧客管çãã¸ã㯠{ public static IList<Customer> åæ¹ä¸è´ã§å§åãæ¤ç´¢ãã(string head) { var list = new List<Customer>(); if(string.IsNullOrWhiteSpace(head)) return list; //å¼æ°ã空æåã®ã¨ãã¯ã空ã®ãªã¹ããè¿ãã // DB ã¢ã¯ã»ã¹ using(SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Samples\Northwind.sdf")){ conn.Open(); var cmdText = @"SELECT Country, [Company Name], [Customer ID], [Contact Name] FROM Customers WHERE ([Contact Name] LIKE @p1) OR ([Contact Name] LIKE @p2)"; using (DbCommand cmd = new SqlCeCommand(cmdText, conn)) { { var p1 = cmd.CreateParameter(); p1.ParameterName = "@p1"; p1.DbType = System.Data.DbType.String; p1.Value = head + "%"; //ãã¡ã¼ã¹ããã¼ã ã¨ã®åæ¹ä¸è´ cmd.Parameters.Add(p1); } //(å¤æ°p1ã®ã¹ã³ã¼ããããã§åã£ã¦ãã) { var p2 = cmd.CreateParameter(); p2.ParameterName = "@p2"; p2.DbType = System.Data.DbType.String; p2.Value = "% " + head + "%"; //ã©ã¹ããã¼ã ã»ããã«ãã¼ã ã¨ã®åæ¹ä¸è´ cmd.Parameters.Add(p2); } using (DbDataReader reader = cmd.ExecuteReader()) { //ã¯ã¨ãªã¼å®è¡ while (reader.Read()) { //çµæãä¸è¡ãã¤ãªãã¸ã§ã¯ãã«è©°ãæ¿ã var customer = new Customer() { Country = reader.GetString(0), CompanyName = reader.GetString(1), CustomerID = reader.GetString(2), ContactName = reader.GetString(3), }; list.Add(customer); } } } } return list; } }