{"id":2116,"date":"2011-03-17T11:49:57","date_gmt":"2011-03-17T02:49:57","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=2116"},"modified":"2011-03-17T11:49:57","modified_gmt":"2011-03-17T02:49:57","slug":"dbunit-%e3%81%ae-net-%e7%89%88%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%e6%ba%96%e5%82%99","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/2116","title":{"rendered":"DBUnit \u306e .NET \u7248\u3092\u4f5c\u6210\u3059\u308b(\u6e96\u5099)"},"content":{"rendered":"<p>\u5b9f\u306f java \u7248\u306e DBUnit \u306f xml \u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u306b\u884c\u304f\u3057\u3001DBUnit.net \u306f\u52d5\u4f5c\u7684\u306b\u540c\u3058\u306a\u306e\u3067\u3001\u81ea\u524d\u306e DBUnit \u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u540d\u524d\u304c\u30c0\u30d6\u3063\u3066\u3044\u308b\u3051\u3069\u3001\u3053\u308c\u306f\u5f8c\u304b\u3089\u3002<\/p>\n<p>UIDD \u306b\u5247\u3063\u3066\u3001\u6700\u521d\u306f\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u3056\u3063\u304f\u308a\u3068\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u3053\u3093\u306a\u98a8\u306b\u4f7f\u3048\u308b\u3068\u3001\u30de\u30cb\u30e5\u30a2\u30eb\u306a\u3057\u3067\u3082\u4f7f\u3048\u308b\u304b\u3082\u306d\u3001\u3068\u3044\u3046\u96f0\u56f2\u6c17\u3067\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Text;<br \/>\nusing System.Data;<br \/>\nusing System.Data.SqlClient;<br \/>\nusing NUnit.Framework;<br \/>\nusing DBUnit.Framework;<\/p>\n<p>namespace Test.DBUnit<br \/>\n{<br \/>\n\t[TestFixture]<br \/>\n\tpublic class TestDBUnit<br \/>\n\t{<br \/>\n        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u524d\u51e6\u7406<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n\t\t[SetUp]<br \/>\n\t\tpublic void SetUp()<br \/>\n\t\t{<br \/>\n            \/\/ \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u4fdd\u6301<br \/>\n\t\t\tDB.Connection = new SqlConnection(&quot;&quot;);<br \/>\n\t\t}<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u5f8c\u51e6\u7406<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        [TearDown]<br \/>\n        public void TearDown()<br \/>\n        {<br \/>\n            \/\/ \u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u524a\u9664<br \/>\n            DB.Clear();<br \/>\n        }<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ DataTable \u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n\t\t[TestCase]<br \/>\n\t\tpublic void TestSetUp()<br \/>\n\t\t{<br \/>\n\t\t\tDataTable dt = new DataTable();<\/p>\n<p>\t\t\tdt.TableName = &quot;XProduct&quot;;<br \/>\n\t\t\tdt.Columns.Add( new DataColumn(&quot;ID&quot;, typeof(int) ));<br \/>\n\t\t\tdt.Columns.Add( new DataColumn(&quot;Name&quot;, typeof(string) ));<br \/>\n\t\t\tDataRow dr = dt.NewRow();<br \/>\n\t\t\tdr[&quot;ID&quot;] = 10;<br \/>\n\t\t\tdr[&quot;Name&quot;] = &quot;masuda&quot;;<br \/>\n            dt.Rows.Add(dr);<\/p>\n<p>            dr = dt.NewRow();<br \/>\n            dr[&quot;ID&quot;] = 20;<br \/>\n            dr[&quot;Name&quot;] = &quot;tomoaki&quot;;<br \/>\n            dt.Rows.Add(dr);<\/p>\n<p>\t\t\t\/\/ \u524d\u51e6\u7406<br \/>\n\t\t\tDB.Insert(dt);<\/p>\n<p>            DataTable result = new DataTable();<br \/>\n            SqlDataAdapter da = new SqlDataAdapter(<br \/>\n                &quot;SELECT * FROM XProduct&quot;, DB.Connection);<br \/>\n            da.Fill(result);<\/p>\n<p>            Assert.AreEqual(2, result.Rows.Count);<br \/>\n            Assert.AreEqual(10, result.Rows[0][&quot;ID&quot;]);<br \/>\n            Assert.AreEqual(&quot;masuda&quot;, result.Rows[0][&quot;Name&quot;]);<\/p>\n<p>            \/\/ \u5f8c\u51e6\u7406<br \/>\n\t\t\tDB.Truncate(dt);<br \/>\n\t\t}<\/p>\n<p>        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u72ec\u81ea\u306eDBUnit.DBTable \u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n\t\t[TestCase]<br \/>\n\t\tpublic void TestSetup2()<br \/>\n\t\t{<br \/>\n\t\t\tDBTable dt = new DBTable(&quot;XProduct&quot;);<\/p>\n<p>            \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a<br \/>\n\t\t\tdt.SetDefault(<br \/>\n                &quot;ID&quot;, 0,<br \/>\n                &quot;Name&quot;, &quot;&quot;,<br \/>\n                &quot;Update_at&quot;, DateTime.Now);<br \/>\n            \/\/ \u8a2d\u5b9a\u3059\u308b\u5217\u540d<br \/>\n\t\t\tdt.AddColumns(&quot;ID&quot;,&quot;Name&quot;);<br \/>\n            \/\/ \u884c\u3092\u8a2d\u5b9a<br \/>\n\t\t\tdt.AddRow(10, &quot;masuda&quot;);<br \/>\n\t\t\tdt.AddRow(20, &quot;tomoaki&quot;);<br \/>\n            \/\/ insert \u3092\u5b9f\u884c<br \/>\n\t\t\tdt.Insert();<\/p>\n<p>            \/\/ \u30c7\u30fc\u30bf\u3092\u30af\u30ea\u30a2<br \/>\n\t\t\tdt.Clear();<br \/>\n\t\t\tAssert.AreEqual( 0, dt.Rows.Count );<\/p>\n<p>            \/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u8aad\u307f\u8fbc\u307f<br \/>\n\t\t\tdt.Select();<br \/>\n\t\t\tAssert.AreEqual(2, dt.Rows.Count);<br \/>\n\t\t\tAssert.AreEqual(10, dt[0][&quot;ID&quot;]);<br \/>\n\t\t\tAssert.AreEqual(&quot;masuda&quot;, dt[0][&quot;Name&quot;]);<\/p>\n<p>            \/\/ \u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u524a\u9664<br \/>\n\t\t\tdt.Delete();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u5fae\u5999\u306b\u30b3\u30fc\u30c9\u304c\u5197\u9577\u306a\u306e\u306f\u3001.NET v2.0 \u3092\u5bfe\u8c61\u306b\u3057\u3066\u3044\u308b\u304b\u3089\u3067\u3059\u3002\u624b\u5143\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c v2.0 \u306a\u306e\u3067\u305d\u308c\u306b\u5408\u308f\u305b\u3066\u4f5c\u3089\u306a\u3044\u3068\u610f\u5473\u304c\u306a\u3044\u306e\u3067\u3001\u3068\u3044\u3046\u306e\u304c\u7b2c\u4e00\u306e\u7406\u7531\u3002LINQ to SQL \u3068\u30bf\u30d7\u30eb\u306a\u3093\u304b\u3092\u4f7f\u3048\u308b\u3068\u3082\u3046\u5c11\u3057\u30de\u30b7\u306a\u30b3\u30fc\u30c9\u306b\u306a\u308b\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001\u305d\u3053\u306f v2.0 \u304c\u30bf\u30fc\u30b2\u30c3\u30c8\u3068\u3044\u3046\u3053\u3068\u3067\u3002<\/p>\n<p>TestSetUp \u306e\u307b\u3046\u3067\u306f\u3001\u65e2\u5b58\u306e DataTable\/DataRow \u3092\u4f7f\u3063\u3066\u307f\u305f\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u3002\u30ab\u30e9\u30e0\u3092\u4f5c\u308b\u3068\u3053\u308d\u304c\u3084\u3084\u9762\u5012\u306a\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3060\u3068\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u5165\u308c\u308b\u306e\u304c\u5927\u5909\u306a\u306e\u3067\u3001\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u4f5c\u308b\u306e\u304c\u82e6\u52b4\u3057\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001TestSetup2 \u306e\u3088\u3046\u306b\u3001\u3079\u305f\u3079\u305f\u306a\u4ed5\u69d8\u306b\u3059\u308b\u306e\u304c\u3044\u3044\u304b\u306a\u3068\u3002<br \/>\n\u6240\u8a6e NUnit \u3068\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306b\u3057\u304b\u4f7f\u308f\u306a\u3044\u306e\u3067\u3001\u5206\u304b\u308a\u3084\u3059\u3044\u307b\u3046\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002\u578b\u4ed8\u306e DataTable \u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3082\u8003\u3048\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u306f\u74b0\u5883\u4f9d\u5b58\uff08\u3068\u3044\u3046\u304b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u65b9\u91dd\u4f9d\u5b58\uff09\u306b\u306a\u308a\u305d\u3046\u306a\u306e\u3067\u3001DataTable \u98a8\u306b\u6271\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u3056\u3063\u304f\u308a\u3068\u30af\u30e9\u30b9\u3068\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u3063\u3066\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u901a\u308b\u3088\u3046\u306b\u3057\u305f\u306e\u304c\u4e0b\u8a18\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Text;<br \/>\nusing System.Data;<br \/>\nusing System.Data.SqlClient;<\/p>\n<p>namespace DBUnit.Framework<br \/>\n{<br \/>\n\tpublic class DB<br \/>\n\t{<br \/>\n        public static SqlConnection Connection;<br \/>\n\t\tpublic static void Insert(DataTable dt)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic static void Delete(DataTable dt)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic static void Truncate(DataTable dt)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n        public static void Clear()<br \/>\n        {<br \/>\n        }<br \/>\n\t}<\/p>\n<p>\tpublic class DBRow<br \/>\n\t{<br \/>\n\t\tpublic int Count;<br \/>\n\t\tpublic object this[string key]<br \/>\n\t\t{<br \/>\n\t\t\tget<br \/>\n\t\t\t{<br \/>\n\t\t\t\treturn null;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic class DBTable<br \/>\n\t{<br \/>\n\t\tpublic string TableName;<br \/>\n\t\tpublic List&lt;DBRow&gt; Rows;<\/p>\n<p>        public DBTable(string name)<br \/>\n        {<br \/>\n            this.TableName = name;<br \/>\n        }<\/p>\n<p>\t\tpublic void SetDefault(params object[] ps)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void AddColumns(params string[] columns)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void AddRow(params object[] rows)<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void Insert()<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void Clear()<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void Select()<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic void Delete()<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t\tpublic DBRow this[ int index ]<br \/>\n\t\t{<br \/>\n\t\t\tget<br \/>\n\t\t\t{<br \/>\n\t\t\t\treturn this.Rows[index];<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u305d\u3057\u3066\u3001DBUnit \u3092 NUnit \u3092\u4f7f\u3063\u3066\u4f5c\u6210\u3059\u308b\u3001\u3068\u3044\u3046\u4e0d\u601d\u8b70\/\u5f53\u305f\u308a\u524d\u306a\u3053\u3068\u3092\u3084\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9f\u306f java \u7248\u306e DBUnit \u306f xml \u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u306b\u884c\u304f\u3057\u3001DBUnit.net \u306f\u52d5\u4f5c\u7684\u306b\u540c\u3058\u306a\u306e\u3067\u3001\u81ea\u524d\u306e DBUnit \u3092\u4f5c\u308a\u307e\u3059\u3002 \u540d\u524d\u304c\u30c0\u30d6\u3063\u3066\u3044\u308b\u3051\u3069\u3001\u3053\u308c\u306f\u5f8c\u304b\u3089\u3002 UIDD \u306b\u5247\u3063\u3066\u3001\u6700\u521d\u306f &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/2116\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[23,24],"tags":[],"class_list":["post-2116","post","type-post","status-publish","format-standard","hentry","category-csharp","category-xunit"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=2116"}],"version-history":[{"count":1,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2116\/revisions"}],"predecessor-version":[{"id":2117,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2116\/revisions\/2117"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=2116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=2116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=2116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}