在测试组件时遇到的奇怪问题。单步可以执行通过,不单步则出现异常

Equn 2008-10-23 11:28:27
使用VS2005\.Net2.0开发,NUnit 2.4.8进行测试。

测试代码如下:
[Test]
public void TestEmployee()
{
employees emps = new employees();
employee emp=emps.add();
emp.name = "曹熠";
emp.number = "0001";
emp.phone="130456456";
emp.ShopId=1;
emp.save();
Assert.Greater(emp.id, 0); //测试是否增加成功,自动生成ID
Assert.Contains(emp, emps.records.Values); //测试员工列表中是否已增加

Assert.IsTrue(emp.ChangePassword("", "123456")); //新用户第一次修改密码,老密码为空,可以修改成功
Assert.IsTrue(emp.CheckPassword("123456"));


emp.UserPower["p"] = true; //设置用户权限
emp.save(); //更新到数据库中

Assert.IsTrue(emp.UserPower["p"]); //检查刚才修改的权限是否有修改,通过


int empid = emp.id;

employee addemp=new employee(empid); //重新从数据库加载刚才增加的员工,直接运行则上面设置的权限数据不能加载,如在此中断运行单步调试则加载正常。数据库数据正常。
Assert.IsTrue(addemp.UserPower["p"]); //检查刚才修改的权限是否有保存,如果是直接运行则此处为false,单步调试则为true

}

[Test]
public void TestID23Emp()
{
employee emp = new employee(23); //加载以前增加的员工数据则正常
Assert.IsTrue(emp.UserPower["p"]); //检查刚才修改的权限是否有保存
}

不知道为什么?直接运行则员工的权限设置数据丢失,单步调试则不会。而且只是新增加的员工会丢失。
employee类的构造函数如下:

/// <summary>根据主键加载对象构造函数</summary>
public employee(int id)
{
database dbconn = new database();
IDataReader dr = dbconn.ExecuteReader("select * from employee where id=" + id.ToString());
if (dr.Read())
{
this.id = (int)dr["id"];
this.insert_date = (DateTime)dr["insert_date"];
this.name = (string)dr["name"];
this.number = (string)dr["number"];
this.mPassword = (string)dr["password"];
this.phone = (string)dr["phone"];
this.PowerStr = (string)dr["PowerStr"];
this.ShopId = (int)dr["ShopId"];
this.status = (int)dr["status"];
UserPower = new powers(this.PowerStr); //根据权限字符串初始化用户权限对象集合
}
dr.Dispose();
dr = null;
dbconn = null;
}

数据库为ACCESS2000,使用OleDbCommand对象的ExecuteNonQuery方法执行“update”更新SQL。
...全文
144 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
IMAGSE 2008-10-24
  • 打赏
  • 举报
回复
友情帮顶~
松花皮蛋 2008-10-24
  • 打赏
  • 举报
回复
单步测试正确,不单步则出现异常.一般是程序执行时间不匹配,正如楼上所说,加几个SLEEP或者FOR循环延时.
shrinerain 2008-10-24
  • 打赏
  • 举报
回复
也许是时序问题.

你加几个sleep试试.
sxmonsy 2008-10-24
  • 打赏
  • 举报
回复
楼主用.net自带的测试工具测试过没?
Equn 2008-10-24
  • 打赏
  • 举报
回复
是这种情况,但是我没看到那里有资料说OleDbCommand的ExecuteNonQuery是异步执行的啊

OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();

十分感谢你的热情帮助,估计可能是NUnit 2.4.8的原因吧

[Quote=引用 9 楼 findcaiyzh 的回复:]
惭愧,还真是看不出有什么问题。
只能使用console.write打印debug信息了,看看哪里出的问题。

有没有可能读取数据的时候数据库还没有更新完毕?
[/Quote]
Equn 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wartim 的回复:]
是不是有线程
[/Quote]

没有线程
宝_爸 2008-10-24
  • 打赏
  • 举报
回复
惭愧,还真是看不出有什么问题。
只能使用console.write打印debug信息了,看看哪里出的问题。

有没有可能读取数据的时候数据库还没有更新完毕?
wartim 2008-10-24
  • 打赏
  • 举报
回复
是不是有线程
Equn 2008-10-24
  • 打赏
  • 举报
回复
emp.UserPower["p"] 的设置值有保存到数据库中,但重新加载对象时该值丢失,TestID23Emp测试指定ID的员工信息时可以加载正常。

以下是employee类的保存函数:

/// <summary>保存记录</summary>
public bool save()
{
//保存数据入数据库代码
database dbconn = new database();
string sql;
this.PowerStr = UserPower.GetPowerStrs(); //获取用户的权限字符串
filterString(); //过虑非法字符
if (!CheckField()) { return false; } //检测重要字段值是否合法

if (this.id == 0)
{
sql = "insert into employee(";
sql = sql + " [insert_date],";
sql = sql + " [name],";
sql = sql + " [number],";
sql = sql + " [phone],";
sql = sql + " [PowerStr],";
sql = sql + " [ShopId],";
sql = sql + " [status],";
sql = sql + " [password]";

sql = sql + ") values (";
sql = sql + "'" + this.insert_date.ToString() + "',";
sql = sql + "'" + this.name.ToString() + "',";
sql = sql + "'" + this.number.ToString() + "',";
sql = sql + "'" + this.phone.ToString() + "',";
sql = sql + "'" + this.PowerStr.ToString() + "',";
sql = sql + "" + this.ShopId.ToString() + ",";
sql = sql + "" + this.status.ToString() + ",";
sql = sql + "''";

sql = sql + ")";
dbconn.ExecuteNonQuery(sql);
this.id = (int)dbconn.ExecuteScalar("SELECT @@IDENTITY");
this.mPassword = ""; //新增加的用户初始密码为空
}
else
{
sql = "update employee set ";
sql = sql + "[insert_date]='" + this.insert_date.ToString() + "',";
sql = sql + "[name]='" + this.name.ToString() + "',";
sql = sql + "[number]='" + this.number.ToString() + "',";
sql = sql + "[phone]='" + this.phone.ToString() + "',";
sql = sql + "[PowerStr]='" + this.PowerStr.ToString() + "',";
sql = sql + "[ShopId]=" + this.ShopId.ToString() + ",";
sql = sql + "[status]=" + this.status.ToString() + "";

sql = sql + " where id=" + this.id.ToString();
dbconn.ExecuteNonQuery(sql);
}
EventArgs r = new EventArgs();
if (employeeSaveHandler != null)
{
employeeSaveHandler(this, r);
}

return true;
}

以下是数据库的连接字符串:

private bool CreateLink()
{
if (conn == null)
{
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\data.mdb";
conn = new OleDbConnection();
conn.ConnectionString = connstr;
}

if (conn.State == ConnectionState.Closed)
{
try
{
conn.Open();
}
catch (Exception e)
{
message = e.Message;
return false;
}
}
return true;
}
宝_爸 2008-10-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Equn 的回复:]
错误来自Assert.IsTrue(addemp.UserPower["p"]的值,不是前面保存的设置值。Power类也没有问题,问题是employee初始化时没有从数据库读取到权限设置信息,数据库的权限设置正确。

引用 3 楼 findcaiyzh 的回复:
什么异常?

在employee addemp=new employee(empid); throw的,还是在Assert.IsTrue(addemp.UserPower["p"]); throw的。
[/Quote]

那就是说emp.UserPower["p"] 没有更新的true了,能看一下你的connectstring和save的代码吗?
Equn 2008-10-24
  • 打赏
  • 举报
回复
错误来自Assert.IsTrue(addemp.UserPower["p"]的值,不是前面保存的设置值。Power类也没有问题,问题是employee初始化时没有从数据库读取到权限设置信息,数据库的权限设置正确。

[Quote=引用 3 楼 findcaiyzh 的回复:]
什么异常?

在employee addemp=new employee(empid); throw的,还是在Assert.IsTrue(addemp.UserPower["p"]); throw的。
[/Quote]
Equn 2008-10-24
  • 打赏
  • 举报
回复
是已增加到数据库的自动编号值,database类包装了数据库连接及访问接口。empid值正确、无误。

[Quote=引用 2 楼 relidgon 的回复:]
employee addemp=new employee(empid);
--
empid 是否是已增加到db的值呢?

数据库自动提交事务?
[/Quote]
宝_爸 2008-10-24
  • 打赏
  • 举报
回复
什么异常?

在employee addemp=new employee(empid); throw的,还是在Assert.IsTrue(addemp.UserPower["p"]); throw的。
hao1hao2hao3 2008-10-24
  • 打赏
  • 举报
回复
可能是访问数据库的问题,有冲突吧!
红男爵 2008-10-24
  • 打赏
  • 举报
回复
employee addemp=new employee(empid);
--
empid 是否是已增加到db的值呢?

数据库自动提交事务?
宝_爸 2008-10-24
  • 打赏
  • 举报
回复
感觉还是有隐患,建议增加一些console或者是写入文件的log,看看数据到底是从哪里不对的。

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧