我正在尝试将名为事件的模拟域实例传递给 Controller 的show()方法,但show()找不到相关事件,因此返回null。
请注意,以下代码段仍在进行中。
def "trying to show an event containing malicous code"() {
given: "An event named with malicous code"
mockDomain(Event)
def event = Mock(Event)
event.title >> "<script type=\"text/javascript\">alert(\"XSS\");</script>"
event.id >> 1
// Do I have to actually create a full-blown event considering all
// constraints here?
when: "I try to show that event"
controller.params.id = 1
def result = controller.show()
then: "The resulting title will be encoded HTML"
result.eventInstance.title == event.title.encodeAsHTML()
}
这是 Controller 的show()方法的开始:
def show = {
def eventInstance = Event.get(params.id)
// The event exists
if (eventInstance) {
// some processing here
return [eventInstance: eventInstance, isSubscribed: sub ? true:false, sidebar: 'sidebar']
}
最佳答案
尝试通过以下方式模拟Event对象:
def event = new Event()
event.title = "<script type=\"text/javascript\">alert(\"XSS\");</script>"
event.id = 1 // optional
mockDomain Event, [event]
除非您将
event
实例添加到mockDomain
调用中,否则您将无法使用get
检索它
关于unit-testing - 可以在 Controller 的show()方法中使用模拟域 “instance”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4690547/