[英]Unit Testing a Put With Entity Framework
I am attempting to unit test a PUT Request by checking values. 我试图通过检查值对PUT请求进行单元测试。 However, I run into one simple issue.
但是,我遇到了一个简单的问题。 I have a test context like such:
我有这样的测试上下文:
class TestAppContext : ContextInterface
{
public DbSet<User> Users {get; set;}
public DbSet<Request> Requests { get; set; }
public TestAppContext()
{
this.Users = new TestUsersDbSet();
this.Requests = new TestRequestsDbSet();
}
public int SaveChanges(){
return 0;
}
public void MarkAsModified(Object item) {
}
public void Dispose() { }
}
When running a PUT with a DbContext the Entry(item).State
is set to EntityState.Modified
in the MarkAsModified
method, then changes the changes are saved. 当运行具有一个的DbContext的PUT
Entry(item).State
设为EntityState.Modified
在MarkAsModified
方法,则改变所述改变被保存。 How do I emulate this in my test context so that the DbSet reflects the changes from the PUT request? 如何在测试上下文中模拟此内容,以便DbSet反映来自PUT请求的更改?
I've gotten as far as doing this: 我已经做到了这一点:
public void MarkAsModified(Object item) {
if (item.GetType() == typeof(User))
{
}
else if (item.GetType() == typeof(Request))
{
}
}
So that I can determine what is being modified, but how do I actually save the changes into the DbSet for that record? 这样我可以确定正在修改的内容,但是实际上如何将更改保存到该记录的DbSet中呢?
Both records are identified on a variable id
which is an int
. 两条记录都在变量
id
上标识,该变量id
为int
。
In your test context just keep a List<Object> markedAsModified
field, then in the call to MarkAsModified
add the object to that list if it doesn't already exist. 在测试上下文中,只需保留一个
List<Object> markedAsModified
字段,然后在对MarkAsModified
的调用中MarkAsModified
对象添加到该列表(如果尚不存在)。 Then in your test you can have Assert
statements the check the contents of that list to make sure the right objects were passed to that function.. 然后,在测试中,可以使
Assert
语句检查该列表的内容,以确保将正确的对象传递给该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.