如何创建多个scenarii而无需在RSpec的2次测试之间重新启动上下文?我不需要上下文重新初始化(这非常慢),但我需要针对相同的给定上下文检查多个内容。下面的示例有效,但这是一个不好的例子:上下文重新初始化。如果我之前(:all),它由于存根而无效。有什么想法吗?
feature 'Aids page' do
context 'No active user' do
before(:each) do
create_2_different_aids
disable_http_service
visit aids_path
end
after(:each) do
enable_http_service
end
scenario 'Should display 2 aids NOT related to any eligibility' do
display_2_aids_unrelated_to_eligibility
end
scenario 'Should not display breadcrumb' do
expect(page).not_to have_css('.c-breadcrumb')
end
end
end
答案 0 :(得分:1)
功能规格通常在同一场景中具有多个预期。它们不像单元测试,每个it
应该只测试一件事......它们更像用户在页面上实际做的事情:"到这里,点击这个东西,检查我能看到那件事,点击那里,检查事情的变化"等...所以你可以这样做:
feature 'Aids page' do
context 'No active user' do
scenario 'Sees aids not related to eligibility' do
create_2_different_aids
disable_http_service
visit aids_path
expect(page).not_to have_css('.c-breadcrumb')
display_2_aids_unrelated_to_eligibility
enable_http_service
end
end
end
或者......可以使用共享设置(如您所做)。这很常见。
答案 1 :(得分:0)
实测值。实际上有一个hack可以用来初始化你的测试只有一次,如下例所示:
class GalleryAdapter(private val context: Context, var list: ArrayList<Product>, val imageDirectory: File) : RecyclerView.Adapter<GalleryViewHolderItem>() {
var activityIsActive = true
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): GalleryViewHolderItem {
val view = LayoutInflater.from(context).inflate(R.layout.rv_gallery, parent, false)
return GalleryViewHolderItem(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: GalleryViewHolderItem?, position: Int) {
holder?.let {
val imageName = list[position].image.substringAfterLast("/")
val imageFile = File(imageDirectory.path+"/"+imageName)
if (imageFile.exists()) {
Glide.with(context)
.load(imageFile)
.placeholder(R.drawable.ic_image_lightgray)
.error(R.drawable.ic_image_broken_lightgray)
.into(holder.ivProduct)
} else {
HttpClient(context).imageRetrofitClient
.downloadProductImage(list[position].image)
.enqueue(object : Callback<ResponseBody?> {
override fun onFailure(call: Call<ResponseBody?>?, t: Throwable?) {
t?.printStackTrace()
if (activityIsActive) {
Glide.with(context)
.load(R.drawable.ic_image_broken_lightgray)
.into(holder.ivProduct)
}
}
override fun onResponse(call: Call<ResponseBody?>?, response: Response<ResponseBody?>?) {
if (response!!.isSuccessful) {
writeResponseBodyToDisk(response.body()!!, imageFile)
callMediaScanner(imageFile)
if (activityIsActive) {
Glide.with(context)
.load(imageFile)
.into(holder.ivProduct)
}
}
}
})
}
}
}
private fun writeResponseBodyToDisk(body: ResponseBody, file: File) : Boolean {
try {
var inputStream: InputStream? = null
var outputStream: OutputStream? = null
try {
val fileReader = ByteArray(4096)
inputStream = body.byteStream()
outputStream = FileOutputStream(file)
while (true) {
val read = inputStream!!.read(fileReader)
if (read == -1) {
break
}
outputStream.write(fileReader, 0, read)
}
outputStream.flush()
return true
} catch (e: IOException) {
return false
} finally {
if (inputStream != null) {
inputStream.close()
}
if (outputStream != null) {
outputStream.close()
}
}
} catch (e: IOException) {
return false
}
}
private fun callMediaScanner(file: File) {
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.data = Uri.fromFile(file)
context.sendBroadcast(intent)
}
}
class GalleryViewHolderItem(itemView: View) : RecyclerView.ViewHolder(itemView) {
val ivProduct = itemView.ivProduct
val backDark = itemView.backDark
val btUnselect = itemView.ivUnselect
val tvName = itemView.tvName
val tvPrice = itemView.tvPrice
}