在构建我最新的ASP.NET MVC项目时,我开始进入单元测试,依赖注入和所有爵士乐.
我现在想要单元测试我的控制器,我很难搞清楚如何在没有IoC容器的情况下正确地执行此操作.
以一个简单的控制器为例:
public class QuestionsController : ControllerBase
{
private IQuestionsRepository _repository = new SqlQuestionsRepository();
// ... Continue with various controller actions
}
Run Code Online (Sandbox Code Playgroud)
由于它直接实例化SqlQuestionsRepository,因此该类不是单元可测试的.那么,让我们沿着Dependancy Injection路线行进并做:
public class QuestionsController : ControllerBase
{
private IQuestionsRepository _repository;
public QuestionsController(IQuestionsRepository repository)
{
_repository = repository;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎更好.我现在可以使用模拟IQuestionsRepository轻松编写单元测试.但是,现在要实例化控制器的是什么?在调用链SqlQuestionRepository的某个地方必须进行实例化.似乎我只是简单地将问题转移到别处,而不是摆脱它.
现在,我知道这是一个很好的例子,IoC容器可以通过为我配置控制器依赖性来帮助您,同时保持我的控制器易于单元测试.
我的问题是,如何在没有 IoC容器的情况下对这种性质的东西进行单元测试?
注意:我不反对IoC容器,我很快就会走这条路.但是,我很好奇对于那些不使用它们的人来说,替代方案是什么.
asp.net-mvc unit-testing controller ioc-container inversion-of-control
我有一个名为AuctionsController的控制器.在其中我有一些名为Index()和AuctionCategoryListing()的动作:
//Used for displaying all auctions.
public ActionResult Index()
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions();
return View(auctions);
}
//Used for displaying auctions for a single category.
public ActionResult AuctionCategoryListing(string categoryName)
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions()
.Where(c => c.Subcategory.Category.Name == categoryName);
return View("Index", auctions);
}
Run Code Online (Sandbox Code Playgroud)
正如您所知,它们都调用相同的View(此动作称为"调用视图".它的名称是什么?).
@model IEnumerable<Cumavi.Models.Auction>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
IDSubcategory
</th>
<th>
IDCity
</th>
<th>
IDPerson …Run Code Online (Sandbox Code Playgroud) 使用scala选项创建项目时,是否可以在play框架项目中同时具有java和scala控制器和视图?
如何将text/xml读入ASP.MVC控制器上的操作?
我有一个Web应用程序可以从两个不同的源接收POSTed Xml,因此Xml的内容可能不同.
我希望我的控制器上的默认操作能够读取Xml但是我很难看到我如何能够首先将Xml放入操作中.
如果Xml是一致的,我可以使用Model Binder,但这不可能.
我一直试图让它工作很长一段时间,但我仍然无法找到解决方案,除了在视图中的监听器中添加一个afterrender.但我希望控制器能够处理它.
有没有人知道如何解决这个问题?
这就是我目前在我的文件中所拥有的内容 http://pastie.org/2751446
controller/App.js
Ext.define('HD.controller.App', {
extend: 'Ext.app.Controller'
,views: [
'core.Header',
'core.Navigation',
'core.Content'
]
,init: function() {
this.control({
'navigation a[id=tab1]': {
click: this.newTab
}
})
}
,newTab: function() {
console.log('Tab 1 should be loaded now');
}
});
Run Code Online (Sandbox Code Playgroud)
视图/型芯/ Navigation.js
Ext.define('HD.view.core.Navigation', {
extend: 'Ext.panel.Panel'
,name: 'navigation'
,alias: 'widget.navigation'
,layout: 'accordion'
,region: 'west'
,width: 200
,title: 'Navigation'
,collapsible: true
,items: [
{
title: 'Title 1'
,html: '<a id="tab1" style="cursor:pointer;">Tab 1</a>'
},
{
title: 'Title 2'
,html: 'Second'
}
]
});
Run Code Online (Sandbox Code Playgroud) 尝试在Magento Pro v1.11中添加group_id单选按钮
以下是
http://phpmagento.blogspot.com/2012/01/how-to-show-customer-group-selecter-in.html和
http://developersindia.info/magento/magento-override-frontend-controller .html,
这是一个点,但group_id没有写入数据库.
我的模块,到目前为止:
目录结构
app/code/local
- WACI
-- Customer
--- controllers
---- AccountController.php
--- etc
---- config.xml
Run Code Online (Sandbox Code Playgroud)
config.xml中
<config>
<modules>
<WACI_Customer>
<version>0.1.0</version>
</WACI_Customer>
</modules>
<global>
<fieldsets>
<customer_account>
<group_id><create>1</create></group_id>
</customer_account>
</fieldsets>
</global>
<frontend>
<routers>
<customer>
<args>
<modules>
<WACI_Customer before="Mage_Customer_AccountController">
WACI_Customer
</WACI_Customer>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>
Run Code Online (Sandbox Code Playgroud)
AccountController.php
<?php
/**
*Customer account controller
*
* @package WACI_Customer
*/
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
class WACI_Customer_AccountController extends Mage_Customer_AccountController
{
/**
* Create …Run Code Online (Sandbox Code Playgroud) 我正在尝试允许用户在我的主页/目标网页上注册该网站.我已将设计注册表复制到我的目标网页视图中 -
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render …Run Code Online (Sandbox Code Playgroud) 尝试了论坛,文档和博客建议.他们聚合语法,对我来说,没有正确保存并通过所需的控制器路由.
总的来说,我有两个共享功能的软件应用程序.为了实现共享功能,我让Rails生成Shared :: Widgets.这个MVC工作正常.使用标准共享/小部件和等路由查看,更新,创建或删除没问题.
然后,为了产品特定的功能,我创建了两个控制器:Product1 :: Widgets和Product2 :: Widgets.两者都继承自Shared :: Widgets控制器.除产品特定布局外,两者都是空的.
这个方案几乎可行.当我路由到product1/widgets时,它将layout设置为product1并调用shared/widgets的index方法.结果是显示带有product1布局的shared/widgets/index.html.erb.类似地,当我路由到product2/widgets时,这会设置product2布局并调用shared/widgets的索引方法.结果是显示带有product2布局的shared/widgets/index.html.erb.完善.
但现在我们到了form_for.因为它实现了rails魔术,所以它真的非常想直接路由到Shared :: Widgets控制器.但这不是我想要的.我希望它路由到适当的产品控制器,以便设置布局.生成form_for的rails是这样的:
form_for(@shared_widget, :html => { :class => "form"}) do |f|
Run Code Online (Sandbox Code Playgroud)
我试过了:
form_for([:product1, @widget], :html => { :class => "form"}) do |f|
Run Code Online (Sandbox Code Playgroud)
但是重复了名称空间(product1_widget_shared_widget_path).
我尝试了以下三种格式,所有这些格式似乎都正确路由,并保存记录,但记录为空(列为空):
form_for(@widget, url => "product1/widget", :html => { :class => "form"}) do |f|
form_for(@widget, url => url_for(:controller => "widget"), :html => { :class => "form"}) do |f|
form_for(@widget, url => url_for(:controller => "widget", :action => "create"), :html => { :class => "form"}) do |f| …Run Code Online (Sandbox Code Playgroud) 案例: 我有一个组织对象.它有一个Department Objects列表,每个Department都有一个Employee Objects列表.
在JSP中,我有一个复选框列表,它将一个复选框绑定到一个雇员对象(深层次的2个层次结构.即Organization-> Department-> Employee).
<input type="checkbox" name="adminDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
Run Code Online (Sandbox Code Playgroud)
如你看到的:
adminDepartmentList[0].employeeList --> John
adminDepartmentList[2].employeeList --> Rose
Run Code Online (Sandbox Code Playgroud)
绑定很好.提交表单后,在控制器中,我可以遍历admin departmentList并查找创建的所有部门,并查找由于复选框选择而创建的员工.
问题:(部门是使用空名称和非null employeeList创建的.我找不到employeeList所属部门的名称:(那么如何传递一些部门名称,以便将名称注入部门(如正在创建)就像".employeeList"被注入部门一样.
细节: 为您提供我的工作细节:
Organization类有两个Departments列表.Department类有一个Employees列表.员工有姓名和小时工作.
public class Organization{
private long id;
private String name;
private List<Department> adminDepartmentList; //n admin departments
private List<Department> employeeDepartmentList //m employee departments
// default constructor and all getters and setters
}
public class Department{
private long id;
private String name;
private List<Employee> employeeList; //k employees
//default constructor and all getters and setters …Run Code Online (Sandbox Code Playgroud) 我需要在Spring控制器中返回图像.我在这个Spring MVC中尝试回答:如何在@ResponseBody中返回图像?但它不起作用
我的代码是这样的
@RequestMapping(value = "cabang/photo", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> getPhoto() throws IOException {
File imgPath = new File("D:\\test.jpg");
byte[] image = Files.readAllBytes(imgPath.toPath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
headers.setContentLength(image.length);
return new ResponseEntity<>(image, headers, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
但是当我在浏览器中访问它时,它没有显示任何内容(只是没有图片图标).但是如果我读取图像字节数组,它就不是空的.我的代码中是否有任何遗漏?
controller ×10
asp.net-mvc ×3
java ×2
spring ×2
view ×2
action ×1
devise ×1
extjs ×1
extjs4 ×1
form-for ×1
image ×1
jsp ×1
magento ×1
php ×1
rest ×1
scala ×1
spring-mvc ×1
spring-rest ×1
unit-testing ×1
xml ×1