我有一个长的JSON文件(约10000行),我想将其用于测试目的(而不是每次都访问外部服务器:<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/_20sdp" />
<gradient android:startColor="@color/ThemeColor"
android:endColor="@color/ThemeColor"
android:centerColor="@color/ThemeColor"/>
</shape>
在控制器中替代httparty帖子的正确语法是什么
response_test.json
带有对文件的引用?该文件应该存储在哪里?
答案 0 :(得分:1)
extension EditLocalPhotosCollectionCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesPicked.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
photoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCellID", for: indexPath) as! EditLocalPhotoCell
photoCell.backgroundColor = currentTheme.backgroundColor
photoCell.photoView.image = imagesPicked[indexPath.row]
photoCell.photoView.layer.masksToBounds = false
photoCell.photoView.layer.cornerRadius = photoCell.photoView.frame.width / 16
photoCell.photoView.clipsToBounds = true
let width: CGFloat = 20
deleteButton.setImage(#imageLiteral(resourceName: "close"), for: .normal)
deleteButton.frame = CGRect(x: photoCell.photoView.frame.width - width / 2, y: 0, width: width, height: width)
deleteButton.addTarget(self, action: #selector(deletePhoto), for: .touchUpInside)
photoCell.addSubview(deleteButton)
let longPressure = UILongPressGestureRecognizer(target: self, action: #selector(deletePhoto))
photoCell.isUserInteractionEnabled = true
photoCell.addGestureRecognizer(longPressure)
return photoCell
}
@objc func deletePhoto() {
isEditingModeActive = true
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 70, height: 70)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == (CreateLocalTVC.imagesPicked.index(of: #imageLiteral(resourceName: "addPhotos"))) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ADD_PHOTOS_SELECTED"), object: nil)
}
if isEditingModeActive && indexPath.row != (CreateLocalTVC.imagesPicked.index(of: #imageLiteral(resourceName: "addPhotos"))) {
print("REMOVED ITEM AT: \(indexPath)")
collectionView.deleteItems(at: [IndexPath(item: indexPath.row, section: 0)])
imagesPicked.remove(at: indexPath.row)
collectionView.reloadData()
}
}
}
答案 1 :(得分:0)
JSON模拟文件最好存储在fixtures
目录中。如果您使用的是RSpec,则可以将文件存储在spec/fixtures/mocks
中。
如果要在测试环境中模拟API调用,请按以下步骤操作:
添加以下内容以在spec/spec_helper
中配置webmock以禁用所有外部API调用。 Webmock将引发错误并建议如何对相应的API调用进行存根。
# spec/spec_helper.rb
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
然后,在测试中进行API调用之前,先将请求返回response_test.json
作为正文
stub_request(:post, 'https://test.com/search')
.to_return(headers: { 'Content-Type': 'application/vnd.api+json; charset=utf-8' },
body: response_test.json)
result = HTTParty.post(
'https://test.com/search',
:body => [...]
# Assert that result matches some value in the JSON content
expect(result.some_attribute).to eq(some_value)
要进行一些清理,您还可以将stub_request
块提取到方法中,并在API调用之前调用该方法。