NSubstitute HttpPostedFileBase 另存为
NSubstitute HttpPostedFileBase SaveAs
在我对代码进行单元测试的过程中,我有以下代码:
var ufile = Substitute.For<HttpPostedFileBase>();
var server = Substitute.For<HttpServerUtilityBase();
var saved = false;
ufile.FileName.Returns("somefileName");
var fileName = fs.Path.GetFileName(ufile.FileName);
var path = fs.Path.Combine(server.MapPath(upath), fileName);
ufile.When(h => h.SaveAs(path)).Do(x => saved = true);
Assert.IsTrue(saved);
下面是我从不同网站收集的测试内容:
public ActionResult UploadFiles()
{
var fileinfo = new List<UploadedImageViewModel>();
foreach (string files in Request.Files)
{
var hpf = Request.Files[files] as HttpPostedFileBase; //
if (hpf != null && hpf.ContentLength > 0)
continue;
var FileName = Path.GetFileName(hpf.FileName); //Gets name of file uploaded
var temppath = Path.Combine(Server.MapPath("~/uploadtemp/"), FileName); // creates a string representation of file location
hpf.SaveAs(temppath);
//resize the image before you save it to the right folder under FileUploads
}
return View(fileinfo);
}
有人可以帮我理解 Nsubstitute 的 when().Do() 语法吗?在文档中,它说 do 应该有一个动作,但我需要示例来理解。
然后HttpPostedFileBase的SaveAs()方法是无效的,在Nsubstitute Docs中,它说使用when().Do()作为无效方法所以请告诉我我的单元测试有什么问题。
//Suppose we have this setup
public class MyClass
{
string ReturnSomething()
{
return "FooBar";
}
void DoSomething(out string reason){
reason = 'Oops';
}
}
NSubstitute 的常用存根语法是这样使用 Returns
:
myClass.ReturnSomething().Returns("wibble");
这会删除 ReturnSomething()
,但 Returns
语法仅适用于具有 return 值的方法。
对于没有 return 的方法,我们可以使用 When().Do()
。这基本上就是他们文档中 Action
的含义(与 Func
相对,后者确实具有 return 值)。这样做的一个常见需要是填写此类方法的输出参数:
string reason;
myClass.When(c => c.DoSomething(out reason))
.Do(args =>
{
args[0] = "Fail";
});
有关 Action 和 Func 的更多信息,请参阅 MSDN:Action, Func。
在单元测试的特定情况下,不要在调用 SaveAs
时设置变量 saved
,而是考虑使用 NSubstitute.Received 结构进行断言。
在我对代码进行单元测试的过程中,我有以下代码:
var ufile = Substitute.For<HttpPostedFileBase>();
var server = Substitute.For<HttpServerUtilityBase();
var saved = false;
ufile.FileName.Returns("somefileName");
var fileName = fs.Path.GetFileName(ufile.FileName);
var path = fs.Path.Combine(server.MapPath(upath), fileName);
ufile.When(h => h.SaveAs(path)).Do(x => saved = true);
Assert.IsTrue(saved);
下面是我从不同网站收集的测试内容:
public ActionResult UploadFiles()
{
var fileinfo = new List<UploadedImageViewModel>();
foreach (string files in Request.Files)
{
var hpf = Request.Files[files] as HttpPostedFileBase; //
if (hpf != null && hpf.ContentLength > 0)
continue;
var FileName = Path.GetFileName(hpf.FileName); //Gets name of file uploaded
var temppath = Path.Combine(Server.MapPath("~/uploadtemp/"), FileName); // creates a string representation of file location
hpf.SaveAs(temppath);
//resize the image before you save it to the right folder under FileUploads
}
return View(fileinfo);
}
有人可以帮我理解 Nsubstitute 的 when().Do() 语法吗?在文档中,它说 do 应该有一个动作,但我需要示例来理解。
然后HttpPostedFileBase的SaveAs()方法是无效的,在Nsubstitute Docs中,它说使用when().Do()作为无效方法所以请告诉我我的单元测试有什么问题。
//Suppose we have this setup
public class MyClass
{
string ReturnSomething()
{
return "FooBar";
}
void DoSomething(out string reason){
reason = 'Oops';
}
}
NSubstitute 的常用存根语法是这样使用 Returns
:
myClass.ReturnSomething().Returns("wibble");
这会删除 ReturnSomething()
,但 Returns
语法仅适用于具有 return 值的方法。
对于没有 return 的方法,我们可以使用 When().Do()
。这基本上就是他们文档中 Action
的含义(与 Func
相对,后者确实具有 return 值)。这样做的一个常见需要是填写此类方法的输出参数:
string reason;
myClass.When(c => c.DoSomething(out reason))
.Do(args =>
{
args[0] = "Fail";
});
有关 Action 和 Func 的更多信息,请参阅 MSDN:Action, Func。
在单元测试的特定情况下,不要在调用 SaveAs
时设置变量 saved
,而是考虑使用 NSubstitute.Received 结构进行断言。