我有一個方法我在哪里
JSONArray execute(SonarqubeMaintenanceSetting settings) {
String projectsFilePath = "${settings.currentDirectory}/build/projects.json"
File targetDir = new File(projectsFilePath)
if (!targetDir.exists()) {
String url = SONAR_API_URL 'projects/search?ps=500'
Object response = getProjectList(settings.sonarToken, url)
Object[] responseArr = []
if (response != null && response.components != null && response.paging.total != null) {
responseArr = response.components
}
JSONArray projectList = new JSONArray(responseArr)
byte[] bytes = projectList.toString().bytes
OutputStream out = new FileOutputStream(projectsFilePath)
out.write(bytes)
}
InputStreamReader inputStreamReader = new InputStreamReader(new
File(projectsFilePath), 'UTF-8')
BufferedReader reader = new BufferedReader(inputStreamReader)
Object obj = jsonSlurper.parse(reader)
JSONArray projectListInFile = new JSONArray(obj)
projectListInFile
}
我正在閱讀路徑 /build/projects.json 中的檔案是否存在。如果存在,則獲取該檔案并轉換為 json 陣列,如果不存在,則從聲納中檢索資料,并在路徑 /build/projects.json 中生成和檔案,然后讀取該檔案。
要為此撰寫測驗用例,我必須始終模擬路徑 /build/projects.json 中不存在該檔案,并且必須回傳回應,其中包含從路徑 src/test/resources/projects.json 讀取資料的 json 陣列是一個虛擬資料。我嘗試過以下方法
class SonarqubeMaintenanceMainTestSpec extends Specification {
@Subject
SonarqubeMaintenanceMain subject
SonarqubeMaintenanceMain instance
def setup() {
subject = new SonarqubeMaintenanceMain()
subject.with {
instance = Mock(SonarqubeMaintenanceMain)
}
instance = subject.instance
GroovyMock(File, global:true)
}
void "Test execute with settings"() {
given:
File dir = Mock()
File file = new File("src/test/resources/projects.json") // this will be read as null if GroovyMock(File, global:true) is set
when:
JSONArray listOfProject = subject.execute(settings)
then:
1 * new File("${settings.currentDirectory}/build/projects.json") >> dir
1 * mockFile.exists() >> false
1 * new File("${settings.currentDirectory}/build/projects.json") >> file
它能夠模擬檔案,但我無法從路徑 src/test/resources/projects.json 讀取檔案的行
File file = new File("src/test/resources/projects.json")
如果我洗掉 GroovyMock(File, global:true)
,那么我無法模擬 File.exists() 但能夠使用 File file = new File("src/test/resources/projects.json") 讀取檔案
如何使用 spock 在 groovy 的方法級別模擬檔案?
uj5u.com熱心網友回復:
正如您可以控制settings.currentDirectory
的那樣,我建議使用TempDir擴展名并使用真實的檔案系統。
class SonarqubeMaintenanceMainTestSpec extends Specification {
@TempDir
File settingsDir
void "Test execute with settings"() {
given:
def projectJson = new File(settingsDir, 'build/projects.json').tap { parent.mkdirs() }
projectJson.text = SonarqubeMaintenanceMainTestSpec.getResource('/projects.json').text
and:
def settings = new Settings(currentDirectory: settingsDir.absolutePath)
when:
JSONArray listOfProject = subject.execute(settings)
uj5u.com熱心網友回復:
我同意倫納德的觀點,如果存在其他選擇,你應該避免全域模擬,這就是這種情況。全域模擬僅適用于被測 Groovy 代碼,這也限制了它們的使用。
OTOH,很高興探索我們可以用它們做什么,所以讓我們這樣做。這是您正在測驗的 Groovy(!) 類的簡化版本:
class UnderTest {
boolean isFound() {
// Current directory should always exist -> true
new File(".").exists()
}
}
假設我們只想模擬exists()
并且不能注入File
實體,但所有其他File
操作,例如建構式呼叫和其他方法呼叫應該繼續起作用,我們可以使用全域 Groovy spy,如下所示:
import spock.lang.Specification
import spock.lang.Subject
class SonarqubeMaintenanceMainTest extends Specification {
@Subject
UnderTest subject
def setup() {
subject = new UnderTest()
GroovySpy(File, constructorArgs: ["x"], global: true) {
exists() >> false
}
}
void "Test execute with settings"() {
given:
File file = new File("src/test/resources/test.txt")
expect:
!subject.isFound()
file.text.startsWith("Lorem ipsum")
}
}
看?可以讀取資源檔案,測驗通過。
現在,如果我們想變得更加花哨,并說我們真的只想回傳一個假的結果,exists()
如果檔案實體真的是被測類中組態檔路徑名的那個,即在我們的示例中"."
?在這種情況下,我們需要以某種方式獲取File
實體,因為我們不能只this
在存根閉包中說。但是我們可以從閉包委托中獲取模擬物件,然后我們可以從那里獲取檔案實體并呼叫其他類似getName()
的方法,以確定它是否是我們要為其回傳假結果的實體。
孩子們,不要在家里這樣做:
def setup() {
subject = new UnderTest()
GroovySpy(File, constructorArgs: ["x"], global: true) {
exists() >> {
delegate.mockObject.instance.name == "." ? false : callRealMethod()
}
}
}
void "Test execute with settings"() {
given:
File file = new File("src/test/resources/test.txt")
expect:
!subject.isFound()
file.exists()
file.text.startsWith("Lorem ipsum")
}
請注意塊file.exists()
中的附加內容expect:
。在無條件的原始存根版本>> false
中,此條件將失敗。現在它通過了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452774.html
上一篇:存根吸氣劑,而不僅僅是設定值
下一篇:反應測驗庫不更新狀態