0
@Transactional
class service {
def sessionFactory
def doSomething(buildings) {
buildings.each {
def building ->
building.owner = 'John'
building.save(failOnError:true)
}
def session = sessionFactory.getCurrentSession()
checkNotNull(session)
session.flush()
}
}
該代碼使所有的樓宇業主「約翰」或無的Grails /斯波克單元測試與SessionFactory的
但在單元測試代碼,我不能使用@Mock(SessionFactory的),因爲它是不在grails env。如果我莫克(SessionFactory的),如代碼:
given:
service.sessionFactory = Mock(SessionFactory)
Session session = Mock(Session)
service.sessionFactory.getCurrentSession() >> session
Building building = new Building(communityId: 199).save(flush: true, failOnError: true)
when:
service.doSomething(Lists.newArrayList(building)
building = Building.findById(building.id)
then:
building.owner == 'John'
然而building.owner爲NULL,我覺得會沖洗不應該嘲笑
所以,我應該怎麼辦? thx