Visual Studio2010为我生成了一堆单元测试,这似乎很方便,但我质疑它们是否是真正的单元测试。例如,它生成了一个名为SaveTest的测试,该测试执行以下代码:
User user = new User(); //I have to create a user
user.Save(); //This saves the user to the database.
//Assertions here....
我上面的问题是,从我所读到的内容来看,单元测试应该是独立测试的,所以通过测试Saving to The database,这不是单元测试,或者是我错了,MsTest搞错了吗?注
public bool A (UserRequest foo)
{
ClientRequest boo = B(foo); //Mapping local model to client model
C(boo);
return result;
}
我想为方法A编写单元测试,以测试方法B,但我不希望我的单元测试调用方法C。方法C是一种私有方法,但它调用第三方客户端。我无法在单元测试中设置方法C,因为类型"ClientRequest“在测试用例程序集中没有引用。如何在不将客户端dll的引用添加到我的测试程序集的情况下实现这一点。如何跳过调用方法C?
我是新的单元测试和使用微软的单元测试库以下代码。当我正常执行代码时,它执行得很好,但是当我运行为该代码编写的单元测试时,我会在数据文本初始化时得到一个空引用错误。
下面是一种与我试图测试的方法类似的方法:
public int DeleteXYZTableRecord(int Id) { try { int XYZTableIdDeleted = -1; using (System.Transactions.TransactionScope scope = new System.Transactions.
我有一个继承层次结构,如下所示:
public class Foo{
public virtual IList<string> Messages{get;set;}
}
public class Foo2: Foo{
public bool DoSomething(){
//Evaluate something;
}
}
public class Bar: Foo2{
public override bool DoSomething(){
var res = base.DoSomething();
我使用依赖项注入来模拟类,以便对依赖它们的其他类进行单元测试:
class Foo : IFoo
{
// production code
}
class MockFoo : IFoo
{
// mock so other classes that depend on Foo can be unit tested
}
class Bar
{
public DoSomething()
{
var barValue = 20;
// use dependency injection to get Foo instance.
许多模拟框架都有一个功能来验证方法是否被调用。然而,大多数框架都要求代码遵循依赖注入模式。
我试图测试的代码不使用依赖项注入模式,因此不能注入对象的模拟。
前代码:
public class TestMeClass {
public void TransformMe() { }
}
public abstract class SomeeClass {
public SomeMethod() {
CallMeMethod();
}
private void CallMeMethod() {
TestMeClass testMeC
在我的项目中,我正在为我的ViewModels创建一些单元测试。我并没有遇到什么问题,因为它们大多数都很简单,但是当我在我的另一个ViewModel中有一个新的(未完成的) ViewModel时,我遇到了一个问题。
public class OrderViewModel : ViewModelBase
{
public OrderViewModel(IDataService dataService, int orderId)
{
\\ ...
Payments = new ObservableCollection<PaymentViewM