我已经完成了一些教程和基本的例子,但是我很难为我的控制器编写单元测试.我已经看到代码片段实例化控制器并让角度注入$rootScope对象,而对象又用于scope为控制器创建新对象.但我无法弄清楚为什么ctrl.$scope?未定义:
describe('EmployeeCtrl', function () {
var scope, ctrl, $httpBackend;
beforeEach(inject(function (_$httpBackend_, $rootScope, $controller, $filter) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
ctrl = $controller('EmployeeCtrl', { $scope: scope});
expect(ctrl).not.toBeUndefined();
expect(scope).not.toBeUndefined(); //<-- PASS!
expect(ctrl.$scope).not.toBeUndefined(); //<-- FAIL!
}));
});
Run Code Online (Sandbox Code Playgroud)
我最终使用scope变量而不是ctrl.$scope然后在我的第一次测试时我无法弄清楚如何在我的控制器内单元测试函数变量:
控制器:
function EmployeeCtrl($scope, $http, $filter, Employee) {
var searchMatch = function (haystack, needle) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
破损的单元测试:
it('should search ', function () {
expect(ctrl.searchMatch('numbers','one')).toBe(false);
});
Run Code Online (Sandbox Code Playgroud)
这就是我得到的
TypeError:Object#没有方法'searchMatch' …
有没有人得到HTMLUnit(或HtmlUnitDriver)来使用Android应用程序?
这是问题:我收到以下错误消息:
11-26 16:27:26.617: E/AndroidRuntime(1265): java.lang.NoClassDefFoundError: org/w3c/dom/css/CSSRule
Run Code Online (Sandbox Code Playgroud)
这就是我所做的:我尝试添加对以下链接中列出的jar的引用(在Project Dependencies和Project Transitive Dependencies下 - 仅编译,不包括test jar):
http://htmlunit.sourceforge.net/dependencies.html
然而Eclipse一直在崩溃,然后我发现一些问题说Android SDK中已经包含了一些jar:
xalan, xercesImpl and xml-apis
Run Code Online (Sandbox Code Playgroud)
HttpClient
Run Code Online (Sandbox Code Playgroud)
所以我删除了对这些罐子的引用,只保留以下内容:
commons-codec-1.4.jar
commons-collections-3.2.1.jar
commons-io-2.0.1.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
htmlunit-2.9.jar
htmlunit-core-js-2.9.jar
httpmime-4.1.2.jar
nekohtml-1.9.15.jar
sac-1.3.jar
httpcore-4.1.3.jar
Run Code Online (Sandbox Code Playgroud)
正是在这个时候,应用程序能够运行,但是当执行以下代码行时,我开始得到错误:
final WebClient webClient = new WebClient();
11-26 16:27:26.617: E/AndroidRuntime(1265): java.lang.NoClassDefFoundError: org/w3c/dom/css/CSSRule
Run Code Online (Sandbox Code Playgroud)
我做了一个网络搜索,发现xml-apis-1.3.04.jar包含org/w3c/dom/css/CSSRule,所以我将该引用放回到项目中,但应用程序根本不会构建同样的错误消息在上面的第一个链接中描述:
[2011-11-26 16:39:52 - Myproj] Conversion to Dalvik format failed with error 1
Run Code Online (Sandbox Code Playgroud)
有人可以对此有所了解吗?
我有一个User存储在会话中的对象@SessionAttributes.并且一个直接的方法被装饰,@ModelAttribute以便在会话的值为null时初始化它.
用户类:
@Entity
@Table( name="USER")
public class User implements java.io.Serializable {
private Long id;
private String username;
private String password;
....
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
Run Code Online (Sandbox Code Playgroud)
控制器:
@RequestMapping("/item")
@Controller
@SessionAttributes({"user"})
public class MyController {
Run Code Online (Sandbox Code Playgroud)
@ModelAttribute方法:
@ModelAttribute("user")
public User createUser(Principal principal) {
return userService.findByUsername(principal.getName());
}
Run Code Online (Sandbox Code Playgroud)
除了这个特殊的方法外,它似乎都按预期工作:
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String showItem(@PathVariable("id") Long id, …Run Code Online (Sandbox Code Playgroud) 我正在使用用户注册表单处理Spring MVC + Hibernate + JPA应用程序,我决定使用JSR-303验证程序检查用户名是否已存在于DB中:
public class UniqueUsernameValidator implements ConstraintValidator<VerifyUniqueUsername, String> {
@Autowired
UserService userService;
@Override
public void initialize(VerifyUniqueUsername constraintAnnotation) {
}
@Override
public boolean isValid(String username, ConstraintValidatorContext context) {
return username!=null && userService.findByUsername(username) == null;
}
}
Run Code Online (Sandbox Code Playgroud)
这非常简单,验证在我的控制器上运行良好:
....
public String signup(@Valid @ModelAttribute("newUser") User user, BindingResult newUserBeanResult)
.....
Run Code Online (Sandbox Code Playgroud)
我面临的当前问题是,在我User验证对象后,我打电话:
userService.save(user);
Run Code Online (Sandbox Code Playgroud)
哪个实现CrudRepository,我得到了NullPointerException.出于某种原因,UserService验证过程中被注入的控制器上,但不是当我打电话CrudRepository.save().
我看到了类似的帖子: 当Sessionfactory.getCurrentSession.merge调用时,在ConstraintValidator中@Autowired bean null ,这个: hibernate验证器没有使用autowire, 但我想知道是否有人遇到过这个问题.我认为注入bean来访问验证器上的数据库是相当普遍的.
作为一种解决方法,我添加了一个null的检查,userService但感觉不对.
CrudRepository.save()?pre-insert