首页 新闻 会员 周边 捐助

使用DotNetMock.MockObject的时候出现类型初始值设定项引发异常

0
悬赏园豆:50 [待解决问题]

TestAccessController1.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NUnit.Framework;
 6 using DotNetMock.Framework.Data;
 7 
 8 namespace clsLibTest2.TestAccessController
 9 {
10     [TestFixture, Category("TestAccessController1")]
11     public class TestAccessController1
12     {
13         [Test]
14         public void TestValidUser()
15         {
16             MockLogger3 mocklogger3 = new MockLogger3();
17             mocklogger3.ExpectedName = "AccessControl";
18             mocklogger3.AddExpectedMsg("Checking access for dave to secrets");
19             mocklogger3.AddExpectedMsg("Access granted");
20 
21             // set up the mock database
22             MockDbConnection mockconn = new MockDbConnection();
23             MockCommand mockcmd = new MockCommand();
24             MockDataReader mockdrd = new MockDataReader();
25 
26             mockconn.SetExpectedCommand(mockcmd);
27             mockcmd.SetExpectedCommandText(AccessController1.CHECK_SQL);
28             mockcmd.SetExpectedExecuteCalls(1);
29             mockcmd.SetExpectedParameter(new MockDataParameter("@Name", "admin"));
30             mockcmd.SetExpectedParameter(new MockDataParameter("@Password", "admin"));
31             mockcmd.SetExpectedParameter(new MockDataParameter("@Power", "0"));
32 
33             mockcmd.SetExpectedReader(mockdrd);
34             object[,] rows = new object[1, 1];
35             rows[0, 0] = 1;
36             mockdrd.SetRows(rows);
37 
38             AccessController1 accessController1 = new AccessController1("0", mocklogger3, mockconn);
39             Assert.IsTrue(accessController1.CanAccess("admin", "admin"));
40             mocklogger3.Verify();
41             mockconn.Verify();
42             mockcmd.Verify();
43 
44 
45         }
46     }
47 }
ILogger.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace clsLibTest2.TestAccessController
 7 {
 8     public interface ILogger
 9     {
10         void SetName(string name);
11         void Log(string log);
12     }
13 }
MockLogger3
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using DotNetMock;
 6 
 7 namespace clsLibTest2.TestAccessController
 8 {
 9     public class MockLogger3 : MockObject, ILogger
10     {
11         private ExpectationValue _name = new ExpectationValue("name");
12 
13         private ExpectationArrayList _msgs = new ExpectationArrayList("msgs");
14 
15         // Mock control interface
16         public string ExpectedName
17         {
18             set { _name.Expected = value; }
19         }
20 
21         public void AddExpectedMsg(string msg)
22         {
23             _msgs.AddExpected(msg);
24         }
25 
26 
27         //Implement ILooger interface
28         #region ILogger 成员
29 
30         public void SetName(string name)
31         {
32             _name.Actual = name;
33         }
34 
35         public void Log(string msg)
36         {
37             _msgs.AddActual(msg);
38         }
39 
40         #endregion
41     }
42 }
谢军的主页 谢军 | 初学一级 | 园豆:3
提问于:2012-07-12 13:55
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册