当前位置: 首页 > news >正文

网站编写语言什么好公司查名

网站编写语言什么好,公司查名,网站的建设需要数据库,建筑人才网招聘官网首页1. 测试分为两种及详细介绍测试书籍: 1.1 Unit Test : 单元测试 - test the business logic in your app : 测试应用中的业务逻辑 1.2 UI Test : 界面测试 - test the UI of your app : 测试应用中的界面 1.3 测试书籍网址:《Testing Swift》 https://www.hackingwithswift.c…1. 测试分为两种及详细介绍测试书籍: 1.1 Unit Test : 单元测试 - test the business logic in your app : 测试应用中的业务逻辑 1.2 UI  Test :  界面测试 - test the UI of your app : 测试应用中的界面 1.3 测试书籍网址:《Testing Swift》 https://www.hackingwithswift.com/store/testing-swift 2. ViewModel 单元测试 2.1 创建 ViewModelUnitTestingBootcampViewModel.swift import Foundation import SwiftUI import Combine/// 单元测试 ViewModel class UnitTestingBootcampViewModel: ObservableObject{Published var isPremium: BoolPublished var dataArray: [String] []Published var selectedItem: String? nillet dataService: NewDataServiceProtocolvar cancellable SetAnyCancellable()init(isPremium: Bool, dataService: NewDataServiceProtocol NewMockDataService(items: nil)) {self.isPremium isPremiumself.dataService dataService}/// 添加子项func addItem(item: String){// 为空不往下执行guard !item.isEmpty else { return }self.dataArray.append(item)}/// 选中项func selectItem(item: String){if let x dataArray.first(where: {$0 item}){selectedItem x}else{selectedItem nil}}/// 保存项func saveItem(item: String) throws{guard !item.isEmpty else{throw DataError.noData}if let x dataArray.first(where: {$0 item}){print(Save item here!!! \(x))} else {throw DataError.itemNotFound}}/// 错误信息enum DataError: LocalizedError{case noDatacase itemNotFound}/// 请求返回数据func downloadWithEscaping() {dataService.downloadItemsWithEscaping { [weak self] returnedItems inself?.dataArray returnedItems}}/// 下载用到的组合func downloadWithCombine() {dataService.downloadItemsWithCombine().sink { _ in} receiveValue: { [weak self] returnedItems inself?.dataArray returnedItems}.store(in: cancellable)} }2.2 创建测试文件 当创建项目时没有选择 Include Tests/包含测试 选项时需要添加文件去对应项目不然测试文件会报 No such module XCTest 编译错误 添加单元测试文件: 方法一 : 选择项目 - 菜单栏 Editor - Add Target... - 弹出对话框选择 Test 栏下 - Unit Testing Bundle - 填写信息/可默认 - Finish完成创建单元测试文件。 方法二 : 选择项目点击 PROJECT 列最下的 按钮弹出对话框选择 Test 栏下 后面步骤与上一致 创建单元测试文件 UnitTestingBootcampViewModel_Tests.swift import XCTest import Combine /// 导入项目 testable import SwiftfulThinkingAdvancedLearning// 《Testing Swift》 测试书籍 // 书籍网址: https://www.hackingwithswift.com/store/testing-swift // Naming Structure: test_UnitOfWork_StateUnderTest_ExpectedBehavior - 结构体命名: 测试_工作单元_测试状态_预期的行为 // Naming Structure: test_[struct or class]_[variable or function]_[expected result] - 测试_[结构体 或者 类的名称]_[类中的变量名 或者 函数名称]_[预期结果 预期值] // Testing Structure: Given, When, Then - 测试结构: 给定什么时候然后final class UnitTestingBootcampViewModel_Tests: XCTestCase {/// 解决多次引用相同的类var viewModel: UnitTestingBootcampViewModel?var cancellables SetAnyCancellable()/// 开始设置数据override func setUpWithError() throws {// Put setup code here. This method is called before the invocation of each test method in the class.viewModel UnitTestingBootcampViewModel(isPremium: Bool.random())}/// 结束重置数据override func tearDownWithError() throws {// Put teardown code here. This method is called after the invocation of each test method in the class.viewModel nilcancellables.removeAll()}/// 单元测试函数名根据命名规则命名测试_类名称_是否高质量_应该为真func test_UnitTestingBootcampViewModel_isPremium_shouldBeTrue(){// Givenlet userIsPremium: Bool true// Whenlet vm UnitTestingBootcampViewModel(isPremium: userIsPremium)// ThenXCTAssertTrue(vm.isPremium)}/// 单元测试函数名 根据命名规则命名测试_类名称_是否高质量_应该为假func test_UnitTestingBootcampViewModel_isPremium_shouldBeFalse(){// Givenlet userIsPremium: Bool false// Whenlet vm UnitTestingBootcampViewModel(isPremium: userIsPremium)// ThenXCTAssertFalse(vm.isPremium)}/// 单元测试函数名 根据命名规则命名测试_类名称_是否高品质_注入值func test_UnitTestingBootcampViewModel_isPremium_shouldBeInjectedValue(){// Givenlet userIsPremium: Bool Bool.random()// Whenlet vm UnitTestingBootcampViewModel(isPremium: userIsPremium)// ThenXCTAssertEqual(vm.isPremium, userIsPremium)}/// 单元测试函数名 根据命名规则命名 - 注入值_压力 / for 循环func test_UnitTestingBootcampViewModel_isPremium_shouldBeInjectedValue_stress(){for _ in 0 .. 10 {// Givenlet userIsPremium: Bool Bool.random()// Whenlet vm UnitTestingBootcampViewModel(isPremium: userIsPremium)// ThenXCTAssertEqual(vm.isPremium, userIsPremium)}}/// 单元测试函数名 根据命名规则命名 - 数组_预期值为空func test_UnitTestingBootcampViewModel_dataArray_shouldBeEmpty(){// Given// Whenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Then 断言 判定XCTAssertTrue(vm.dataArray.isEmpty)XCTAssertEqual(vm.dataArray.count, 0)}/// 单元测试函数名 根据命名规则命名 - 数组_预期值添加项func test_UnitTestingBootcampViewModel_dataArray_shouldAddItems(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet loopCount: Int Int.random(in: 1..100)for _ in 0 .. loopCount{vm.addItem(item: UUID().uuidString)}// Then 断言 判定XCTAssertTrue(!vm.dataArray.isEmpty)XCTAssertFalse(vm.dataArray.isEmpty)XCTAssertEqual(vm.dataArray.count, loopCount)XCTAssertNotEqual(vm.dataArray.count, 0)// GreaterThan 大于XCTAssertGreaterThan(vm.dataArray.count, 0)// XCTAssertGreaterThanOrEqual// XCTAssertLessThan// XCTAssertLessThanOrEqual}/// 单元测试函数名 根据命名规则命名 - 数组_预期值添加空白字符func test_UnitTestingBootcampViewModel_dataArray_shouldNotAddBlankString(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenvm.addItem(item: )// Then 断言 判定XCTAssertTrue(vm.dataArray.isEmpty)}/// 单元测试函数名 根据命名规则命名 - 数组_预期值添加空白字符func test_UnitTestingBootcampViewModel_dataArray_shouldNotAddBlankString2(){// Givenguard let vm viewModel else {XCTFail()return}// Whenvm.addItem(item: )// Then 断言 判定XCTAssertTrue(vm.dataArray.isEmpty)}/// 单元测试函数名 根据命名规则命名 - 选中项_预期值开始为空func test_UnitTestingBootcampViewModel_selectedItem_shouldStartAsNil(){// Given// Whenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Then 断言 判定XCTAssertTrue(vm.selectedItem nil)XCTAssertNil(vm.selectedItem)}/// 单元测试函数名 根据命名规则命名 - 选中项_预期值应该为空 当选择无效项func test_UnitTestingBootcampViewModel_selectedItem_shouldBeNilWhenSelectingInvalidItem(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Select valid item : 选择有效项let newItem UUID().uuidStringvm.addItem(item: newItem)vm.selectItem(item: newItem)// Select invalid item : 选择无效项// Whenvm.selectItem(item: UUID().uuidString)// Then 断言 判定XCTAssertNil(vm.selectedItem)}/// 单元测试函数名 根据命名规则命名 - 选中项_预期值应该选中func test_UnitTestingBootcampViewModel_selectedItem_shouldBeSelected(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet newItem UUID().uuidStringvm.addItem(item: newItem)vm.selectItem(item: newItem)// Then 断言 判定XCTAssertNotNil(vm.selectedItem)XCTAssertEqual(vm.selectedItem, newItem)}/// 单元测试函数名 根据命名规则命名 - 选中项_预期值选中_压力测试func test_UnitTestingBootcampViewModel_selectedItem_shouldBeSelected_stress(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet loopCount: Int Int.random(in: 1..100)var itemsArray: [String] []for _ in 0 .. loopCount {let newItem UUID().uuidStringvm.addItem(item: newItem)itemsArray.append(newItem)}// 随机取一个字符串let randomItem itemsArray.randomElement() ?? // 检查字符串不为空XCTAssertFalse(randomItem.isEmpty)vm.selectItem(item: randomItem)// Then 断言 判定XCTAssertNotNil(vm.selectedItem)XCTAssertEqual(vm.selectedItem, randomItem)}/// 单元测试函数名 根据命名规则命名 - 保存项_预期值输出错误异常_元素没找到func test_UnitTestingBootcampViewModel_saveItem_shouldThrowError_itemNotFound(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet loopCount: Int Int.random(in: 1..100)for _ in 0 .. loopCount {vm.addItem(item: UUID().uuidString)}// Then 断言 判定XCTAssertThrowsError(try vm.saveItem(item: UUID().uuidString))XCTAssertThrowsError(try vm.saveItem(item: UUID().uuidString), Should throw Item Not Found error!) { error in// 返回错误let returnedError error as? UnitTestingBootcampViewModel.DataError// 判断错误是否相同XCTAssertEqual(returnedError, UnitTestingBootcampViewModel.DataError.itemNotFound)}}/// 单元测试函数名 根据命名规则命名 - 保存项_预期值输出错误异常_没数据func test_UnitTestingBootcampViewModel_saveItem_shouldThrowError_noData(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet loopCount: Int Int.random(in: 1..100)for _ in 0 .. loopCount {vm.addItem(item: UUID().uuidString)}// Then 断言 判定do {try vm.saveItem(item: )} catch let error {// 返回错误let returnedError error as? UnitTestingBootcampViewModel.DataError// 判断错误是否相同XCTAssertEqual(returnedError, UnitTestingBootcampViewModel.DataError.noData)}}/// 单元测试函数名 根据命名规则命名 - 保存项_预期值保存选项func test_UnitTestingBootcampViewModel_saveItem_shouldSaveItem(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet loopCount: Int Int.random(in: 1..100)var itemsArray: [String] []for _ in 0 .. loopCount {let newItem UUID().uuidStringvm.addItem(item: newItem)itemsArray.append(newItem)}// 随机取一个字符串let randomItem itemsArray.randomElement() ?? // 检查字符串不为空XCTAssertFalse(randomItem.isEmpty)// Then 断言 判定XCTAssertNoThrow(try vm.saveItem(item: randomItem))do {try vm.saveItem(item: randomItem)} catch {XCTFail()}}/// 单元测试函数名 根据命名规则命名 - 下载数据_预期值返回选项func test_UnitTestingBootcampViewModel_downloadWithEscaping_shouldReturnItems(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet expectation XCTestExpectation(description: Should return items after 3 seconds)// dropFirst: 删除第一个发布 数组值因为初始化为空数组取的是第二个数组模拟服务数据返回的数组vm.$dataArray.dropFirst().sink { returnedItems inexpectation.fulfill()}.store(in: cancellables)vm.downloadWithEscaping()// Then 断言 判定 GreaterThan大于// 为了安全获取到值设置等待 5 秒wait(for: [expectation], timeout: 5)XCTAssertGreaterThan(vm.dataArray.count, 0)}/// 单元测试函数名 根据命名规则命名 - 下载数据组合_预期值返回选项func test_UnitTestingBootcampViewModel_downloadWithCombine_shouldReturnItems(){// Givenlet vm UnitTestingBootcampViewModel(isPremium: Bool.random())// Whenlet expectation XCTestExpectation(description: Should return items after a seconds)// dropFirst: 删除第一个发布 数组值因为初始化为空数组取的是第二个数组模拟服务数据返回的数组vm.$dataArray.dropFirst().sink { returnedItems inexpectation.fulfill()}.store(in: cancellables)vm.downloadWithCombine()// Then 断言 判定 GreaterThan大于// 为了安全获取到值设置等待 5 秒wait(for: [expectation], timeout: 5)XCTAssertGreaterThan(vm.dataArray.count, 0)}/// 单元测试函数名 根据命名规则命名 - 下载数据组合_预期值返回选项func test_UnitTestingBootcampViewModel_downloadWithCombine_shouldReturnItems2(){// Givenlet items: [String] [UUID().uuidString, UUID().uuidString, UUID().uuidString, UUID().uuidString]let dataService: NewDataServiceProtocol NewMockDataService(items: items)let vm UnitTestingBootcampViewModel(isPremium: Bool.random(), dataService: dataService)// Whenlet expectation XCTestExpectation(description: Should return items after a seconds)// dropFirst: 删除第一个发布 数组值因为初始化为空数组取的是第二个数组模拟服务数据返回的数组vm.$dataArray.dropFirst().sink { returnedItems inexpectation.fulfill()}.store(in: cancellables)vm.downloadWithCombine()// Then 断言 判定 GreaterThan大于// 为了安全获取到值设置等待 5 秒wait(for: [expectation], timeout: 5)XCTAssertGreaterThan(vm.dataArray.count, 0)XCTAssertEqual(vm.dataArray.count, items.count)} } 3. 模拟请求数据 单元测试 3.1 创建模拟请求数据类 NewMockDataService.swift import Foundation import SwiftUI import Combine/// 定义协议 protocol NewDataServiceProtocol{func downloadItemsWithEscaping(completion: escaping (_ items: [String]) - ())func downloadItemsWithCombine() - AnyPublisher[String], Error }/// 实现模拟请求数据 class NewMockDataService: NewDataServiceProtocol {let items: [String]init(items: [String]?) {self.items items ?? [ONE, TWO, THREE]}/// 模拟网络下载数据 escaping: 转义字符func downloadItemsWithEscaping(completion: escaping (_ items: [String]) - ()) {DispatchQueue.main.asyncAfter(deadline: .now() 2) {completion(self.items)}}/// 下载组合func downloadItemsWithCombine() - AnyPublisher[String], Error {// 数据转换Just(self.items).tryMap({ publishedItems inguard !publishedItems.isEmpty else {throw URLError(.badServerResponse)}return publishedItems}).eraseToAnyPublisher()} } 3.2 创建单元测试类 NewMockDataService_Tests.swift import XCTest import Combine /// 导入项目 testable import SwiftfulThinkingAdvancedLearningfinal class NewMockDataService_Tests: XCTestCase {/// 随时取消控制器var cancellable SetAnyCancellable()override func setUpWithError() throws {// Put setup code here. This method is called before the invocation of each test method in the class.}override func tearDownWithError() throws {// Put teardown code here. This method is called after the invocation of each test method in the class.cancellable.removeAll()}// 单元测试函数名 根据命名规则命名 - 测试_类名_初始化_预期值正确的设置值func test_NewMockDataService_init_doesSetValuesCorrectly() {// 执行// Given: 给定let items: [String]? nillet items2: [String]? []let items3: [String]? [UUID().uuidString, UUID().uuidString]// When: 时间let dataService NewMockDataService(items: items)let dataService2 NewMockDataService(items: items2)let dataService3 NewMockDataService(items: items3)// Then 然后XCTAssertFalse(dataService.items.isEmpty)XCTAssertTrue(dataService2.items.isEmpty)XCTAssertEqual(dataService3.items.count, items3?.count)}// 单元测试函数名 根据命名规则命名 - 测试_类名_下载转换数据项_预期值正确的设置值func test_NewMockDataService_downloadItemsWithEscaping_doesReturnValues() {// 执行// Given: 给定let dataService NewMockDataService(items: nil)// When: 时间var items: [String] []let expectation XCTestExpectation()dataService.downloadItemsWithEscaping { returnedItems initems returnedItemsexpectation.fulfill()}// Then 然后// 等待 5 秒wait(for: [expectation], timeout: 5)// 断言两个数组大小一样XCTAssertEqual(items.count, dataService.items.count)}// 单元测试函数名 根据命名规则命名 - 测试_类名_下载数据项组合_预期值正确的设置值func test_NewMockDataService_downloadItemsWithCombine_doesReturnValues() {// 执行// Given: 给定let dataService NewMockDataService(items: nil)// When: 时间var items: [String] []let expectation XCTestExpectation()// 下载组合控制dataService.downloadItemsWithCombine().sink { completion inswitch completion{case .finished:expectation.fulfill()case .failure:XCTFail()}} receiveValue: {returnedItems in// fulfill: 完成items returnedItems}.store(in: cancellable)// Then 然后// 等待 5 秒wait(for: [expectation], timeout: 5)// 断言两个数组大小一样XCTAssertEqual(items.count, dataService.items.count)}// 单元测试函数名 根据命名规则命名 - 测试_类名_下载数据项组合_预期值确实失败func test_NewMockDataService_downloadItemsWithCombine_doesFail() {// 执行// Given: 给定let dataService NewMockDataService(items: [])// When: 时间var items: [String] []let expectation XCTestExpectation(description: Does throw an error)let expectation2 XCTestExpectation(description: Does throw URLError.badServerResponse)// 下载组合控制dataService.downloadItemsWithCombine().sink { completion inswitch completion{case .finished:XCTFail()case .failure(let error):expectation.fulfill()//let urlError error as? URLError// 断言判定//XCTAssertEqual(urlError, URLError(.badServerResponse))// 错误判断if error as? URLError URLError(.badServerResponse) {expectation2.fulfill()}}} receiveValue: {returnedItems in// fulfill: 完成items returnedItems}.store(in: cancellable)// Then 然后// 等待 5 秒wait(for: [expectation, expectation2], timeout: 5)// 断言两个数组大小一样XCTAssertEqual(items.count, dataService.items.count)} }4. 创建单元测试 View调用测试的 ViewModel UnitTestingBootcampView.swift import SwiftUI/*1. Unit Test : 单元测试- test the business logic in your app : 测试应用中的业务逻辑2. UI Test : 界面测试- test the UI of your app : 测试应用中的界面*//// 单元测试 struct UnitTestingBootcampView: View {StateObject private var vm: UnitTestingBootcampViewModelinit(isPremium: Bool){_vm StateObject(wrappedValue: UnitTestingBootcampViewModel(isPremium: isPremium))}var body: some View {Text(vm.isPremium.description)} }struct UnitTestingBootcampView_Previews: PreviewProvider {static var previews: some View {UnitTestingBootcampView(isPremium: true)} }
http://www.pierceye.com/news/667678/

相关文章:

  • 防止服务器上的网站被进攻app推广兼职
  • 保定电商网站建设国内最好的crm软件
  • 企业网站建设哪家公司好莱芜金点子信息港房产网
  • 个人可以建设网站吗海淀网站建设本溪
  • 宜昌网站建设兼职怎样做自媒体拍视频赚钱
  • 我的世界做视频封面的网站免费的app源码网
  • 网站搭建wordpress参考消息电子版在线阅读
  • 成立一个网站平台要多少钱科技有限公司一般是做什么的
  • 邵阳 网站开发 招聘桂林阳朔楼盘最新价格
  • 如何建设网站导航内链接wordpress 特别慢
  • 蚌埠网站建设文章网站软件定制开发公司
  • 软件通网站建设百度收录网站电话
  • 取消网站备案制度正规的电商平台有哪些
  • 茶叶网站源码php6731官方网站下载
  • 网站建设mfdos 优帮云制作简历哪个网站好
  • 淮南市城乡建设局网站网站seo收费
  • 陕西手机网站制作描述网站开发的广告词
  • 一个网址建多个网站手机网站数据加载
  • 网站视觉分析上海做saas平台网站的公司
  • 沈阳网站设计网站一键制作
  • 建设工程中标查询网站北京建设质量协会网站
  • 做公司网站要素做关于灯饰的网站
  • 网站编辑工具软件单位发购物或电影卡有哪些app
  • dw网站导航怎么做3免费网站建站
  • 用jsp做网站的代码句容网站建设制作
  • 宁国新站seo网页版微信登录提示二维码已失效
  • 深圳英文网站建设去哪家公司电商网站开发
  • 黑色网站后台出库入库管理软件app
  • 网站建设公司团队简介国外有网站备案制度吗
  • 怎么让公司网站随便就搜的到wordpress后台卡顿