我正在使用这个帮助方法将我的PartialViewResult转换为字符串并将其作为Json返回 - http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/
我的问题是我使用Moq来模拟控制器,每当我运行使用这个RenderPartialViewToString()方法的单元测试时,我得到了"对象引用未设置为对象的实例".ControllerContext上的错误.
private ProgramsController GetController()
{
var mockHttpContext = new Mock<ControllerContext>();
mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("test");
mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
// Mock Repositories
var mockOrganizationRepository = new MockOrganizationRepository(MockData.MockOrganizationsData());
var mockIRenderPartial = new BaseController();
var controller = new ProgramsController(mockOrganizationRepository, mockIRenderPartial);
controller.ControllerContext = mockHttpContext.Object;
return controller;
}
Run Code Online (Sandbox Code Playgroud)
这会返回一个代理控制器,也许这就是我收到该错误的原因.知道如何进行单元测试吗?
非常感谢你.
这很奇怪,我不知道为什么.我有一个ViewModel为我的对象返回一些值,在渲染它时,它们有不同的值,但是,它们指向同一个属性:
<%: Model.myProperty %>
Run Code Online (Sandbox Code Playgroud)
返回"25",这是我设置的属性.但是当它作为文本框呈现时,它返回"0"作为我的文本框的值!
<%: Html.TextBoxFor(f => f.myProperty) %>
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?该属性为十进制类型.谢谢.
由于我在使用Moq框架(ASP.NET MVC -使用Moq框架测试RenderPartialViewToString()的单元测试RenderPartialViewToString())时遇到问题,我正在考虑直接获取控制器,而不使用Moq进行这些特定测试,但是,如何在不使用任何Moq框架的情况下为我的测试模拟(或设置)HttpContext?
我需要能够做类似的事情,当然没有Moq:
var mockHttpContext = new Mock<ControllerContext>();
mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("n1\\test");
mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
我有一个类来检索内容,它有一个方法,在检索它之前需要一些输入(过滤器).其中一个"输入"调用另一个方法,它基本上返回一个int,如何使用MOQ模拟它?这是一个例子:
namespace MyNamespace
{
public class ConfigMetaDataColumns : MyModel
{
public int FieldID { get { return ValueInt("FieldID"); } }
public int OrderId { get { return ValueInt("OrderId"); } }
public string Label { get { return ValueString("Label"); } }
public string FieldName { get { return ValueString("FieldName"); } }
public int IsReadOnly { get { return ValueInt("IsReadOnly"); } }
}
public class GetDataClass
{
protected OpenSQLAccessLayer m_WITObject;
// Input Properties
public string GroupID;
public string PageName;
// Output Properties …Run Code Online (Sandbox Code Playgroud) 我正在开发一个供我自己使用的博客系统,并希望实现一个自动完成的标签选择(类似于stackoverflow),我将如何实现这样的东西?任何示例或指向教程的链接将不胜感激.
谢谢.
我有一系列的数字:[1 2 4 8 16 32 64 128]如果我输入一个数字,即66,那么输出应该是64和2.如果我输入87,那么输出应该是64,16,4,2,1.
(基本上,它首先除以最大可能的数字,找到余数,然后继续除以可能的最大数,直到余数为0.或者另一种方式可能只是减去最大可能的数字并继续减去它直到达到0)
我正在考虑一个递归函数,但不是很确定.有帮助吗?
谢谢.
我有一个项目列表,即,List<SearchFilter>这是SearchFilter对象:
public class SearchFilter
{
public int ItemID { get { return ValueInt("ItemID"); } }
public string ItemName { get { return ValueString("ItemName"); } }
public string Type { get { return ValueString("Type"); } }
}
Run Code Online (Sandbox Code Playgroud)
如何Type按组分组,并将分组的项目投影到新的列表中GroupedFilter,即:
public class Filter
{
public int ItemID { get; set; }
public string ItemName { get; set; }
}
public class GroupedFilter
{
public int Type { get; set; }
public List<Filter> Filters { get; …Run Code Online (Sandbox Code Playgroud) 我真的需要有人帮助我完全理解如何与Entity Framework 4 CTP 5,POCO建立多对多的关系.我需要了解3个概念:
这是我目前的型号:
public class MusicSheet
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Key { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Author
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public virtual …Run Code Online (Sandbox Code Playgroud) entity-relationship many-to-many entity-framework poco entity-framework-4
这很奇怪我不知道我做错了什么.我有一个函数来获取日期(即以这种格式:),这06/24/2011是函数:
function checkDate(input){
var d = new Date();
var dspl = input.split("/");
if(dspl.length != 3)
return NaN;
d.setDate(dspl[1]);
d.setMonth(Number(dspl[0])-1);
if(dspl[2].length == 2)
d.setYear("20"+(dspl[2]+""));
else if(dspl[2].length == 4)
d.setYear(dspl[2]);
else
return NaN;
var dt = jsToMsDate(new Date(d));
return dt;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入该月的任何日期,它将正确解析日期,但如果我输入31,即"01/31/2011",它将变为"01/01/2011".我不知道该怎么办,也不确定问题可能在哪里.
如果使用某个构造函数,有没有办法隐藏/显示方法?即:
public class SomeClass
{
public SomeClass(string methodA)
{
}
public SomeClass(int methodB)
{
}
public string MethodA()
{
return "";
}
public int MethodB()
{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
如果SomeClass(string methodA)使用,那么只有MethodA()在我实例化新SomeClass对象时才可用?使用时相同SomeClass(int methodB),那么MethodB()可用吗?
谢谢你们!
我对json结果有点问题.我正在提交联系表单,在提交后,我只想将一些json数据(表示成功或失败并显示消息)返回到视图,而不会导致重定向,但它会将我重定向到动作,这是代码:
HTML
<div id="contactForm2" class="grid_6">
<div id="contactFormContainer">
@using (Html.BeginForm(MVC.Home.ActionNames.ContactForm, MVC.Home.Name, FormMethod.Post, new { id = "contactForm" }))
{
<p>
<input type="text" tabindex="1" size="22" value="" id="contactName" class="text_input required"
name="contactName" />
<label for="contactName">
<strong class="leftSpace">Your Name (required)</strong></label></p>
<p>
<input type="text" tabindex="2" size="22" value="" id="contactEmail" class="text_input required"
name="contactEmail" />
<label for="contactEmail">
<strong class="leftSpace">Email (required)</strong></label></p>
<p>
<input type="text" tabindex="2" size="22" value="" id="contactPhone" class="text_input"
name="contactPhone" />
<label for="contactPhone">
<strong class="leftSpace">Phone</strong></label></p>
<p>
<label>
<strong class="leftSpace n">Message (required)</strong></label>
<textarea tabindex="4" rows="4" cols="56" id="contactMessage" class="text_area required"
name="contactMessage"></textarea></p>
<p> …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
c# ×4
.net ×3
asp.net ×2
jquery ×2
moq ×2
unit-testing ×2
autocomplete ×1
constructor ×1
date ×1
datetime ×1
httpcontext ×1
javascript ×1
json ×1
linq ×1
many-to-many ×1
poco ×1
recursion ×1
series ×1
tagging ×1
tags ×1