Flutter Webアプリで作業していますが、テストを実行できません。
_Flutter 1.7.8+hotfix.4 • channel stable • [email protected]:flutter/flutter.git
Framework • revision 20e59316b8 (9 weeks ago) • 2019-07-18 20:04:33 -0700
Engine • revision fee001c93f
Tools • Dart 2.4.0
_
これはpubspec.yamlです。
_name: web_flutter
description: An app built using Flutter for web
environment:
# You must be using Flutter >=1.5.0 or Dart >=2.3.0
sdk: '>=2.3.0-dev.0.1 <3.0.0'
dependencies:
flutter_web: any
flutter_web_ui: any
provider: any
rxdart: ^0.22.0
http: ^0.12.0+2
json_annotation: ^2.4.0
intl: 0.15.8
dev_dependencies:
build_runner: ^1.4.0
build_web_compilers: ^2.0.0
pedantic: ^1.0.0
json_serializable: ^3.0.0
test: any
flutter:
uses-material-design: true
dependency_overrides:
flutter_web:
git:
url: https://github.com/flutter/flutter_web
path: packages/flutter_web
flutter_web_ui:
git:
url: https://github.com/flutter/flutter_web
path: packages/flutter_web_ui
provider:
git:
url: https://github.com/kevmoo/provider
ref: flutter_web
_
IDEのターミナル(VSコード)またはターミナルアプリで_pub run test
_を実行したときに合格するいくつかのテストを作成しました。
私が問題を抱えているテストでは、プロジェクトからいくつかのパッケージをインポートする必要があります。このテストを実施すると、エラーが発生し、他の2つのテストは実行されません。解決策を示すものは何も見つかりませんでした。
これはエラーメッセージです。
_pub run test MyMac@MyMac
00:06 +0 -1: loading test/service_test.Dart [E]
Failed to load "test/service_test.Dart":
Unable to spawn isolate: file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/ui.Dart:12:8: Error: Not found: 'Dart:html'
import 'Dart:html' as html;
^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine.Dart:11:8: Error: Not found: 'Dart:html'
import 'Dart:html' as html;
^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web/lib/src/services/asset_bundle.Dart:7:8: Error: Not found: 'Dart:html'
import 'Dart:html' show HttpRequest;
^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine.Dart:12:8: Error: Not found: 'Dart:js'
import 'Dart:js' as js;
^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine.Dart:13:8: Error: Not found: 'Dart:js_util'
import 'Dart:js_util' as js_util;
^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/ui/compositing.Dart:20:9: Error: Type 'html.Element' not found.
final html.Element webOnlyRootElement;
^^^^^^^^^^^^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine.Dart:177:37: Error: Type 'html.NodeTreeSanitizer' not found.
class _NullTreeSanitizer implements html.NodeTreeSanitizer {
^^^^^^^^^^^^^^^^^^^^^^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine.Dart:179:21: Error: Type 'html.Node' not found.
void sanitizeTree(html.Node node) {}
^^^^^^^^^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine/bitmap_canvas.Dart:26:9: Error: Type 'html.Element' not found.
final html.Element rootElement = html.Element.tag('flt-canvas');
^^^^^^^^^^^^
file:///Users/MyMac/.pub-cache/git/flutter_web-c04fb502b842859de07e36954a9390465a5426c0/packages/flutter_web_ui/lib/src/engine/bitmap_canvas.Dart:28:3: Error: Type 'html.CanvasElement' not found.
html.CanvasElement _canvas;
^^^^^^^^^^^^^^^^^^
00:06 +0 -1: Some tests failed.
_
この新しいテストをコメント化すると、エラーが解消されません。テストと関連するインポートをコメントアウトすると、残りの2つのテストが実行され、合格します。
これはテストファイルです(問題のあるコードはコメントアウトしたので、見やすくなっています-願っています)。
_import 'package:test/test.Dart';
import 'package:http/http.Dart';
import 'package:http/testing.Dart';
import 'Dart:convert';
import 'package:web_flutter/services/service.Dart';
// These are the package imports that cause errors:
import 'package:web_flutter/model/model.Dart';
import 'package:web_flutter/data/tenant_view_model.Dart';
void main(){
test("getSubmission returns ResponseData{}", () async {
// Arrange: setup the test
final _service = Service();
_service.httpClient = MockClient((request) async {
final responseData = {
"id": "some_id",
"state": "processed",
"test": true,
"editable": false,
"expires_at": "2019-09-19T03:40:22Z",
"processed_at": "2019-09-12T03:40:22Z",
"batch_id": "",
"download_url": "downloadURl.com",
"permanent_download_url": "permanentdownloadURL.com"
};
return Response(json.encode(responseData),200);
});
// Act
final response = await _service.getSubmission("submissionID");
// Assert
expect(response.download_url, "downloadURL.com");
});
test("generateForm returns SubmissionResponse{}", () async {
// Arrange: setup the test
final _service = Service();
_service.httpClient = MockClient((request) async {
final submissionResponse = {
"status": "success",
"submission": {
"id": "some_id",
"state": "pending",
"test": false,
"editable": false,
"expired": false,
"expires_at": null,
"metadata": {
"foo": 123,
"bar": "baz"
},
"processed_at": null,
"batch_id": null,
"data_requests": [],
"download_url": null,
"permanent_download_url": null
}
};
return Response(json.encode(submissionResponse),200);
});
// Act
final response = await _service.generateForm(42, "templateID");
// Assert
expect(response.submission.id, "some_id");
});
test('Tenant View Model generateForm returns tenantVM.submission.submission.id', () async {
// Arrange
final _service = Service();
Tenant tenant;
tenant.id = 42;
_service.httpClient = MockClient((request) async {
final submissionResponse = {
"status": "success",
"submission": {
"id": "some_id",
"state": "pending",
"test": false,
"editable": false,
"expired": false,
"expires_at": null,
"metadata": {
"foo": 123,
"bar": "baz"
},
"processed_at": null,
"batch_id": null,
"data_requests": [],
"download_url": null,
"permanent_download_url": null
}
};
return Response(json.encode(submissionResponse),200);
});
TenantViewModel tenantVM = TenantViewModel(tenant, _service);
// Act
await tenantVM.generateForm("templateID");
// Assert
expect(tenantVM.submission.submission.id, "some_id");
});
}
_
これは、私がテストしようとしているメソッド(generateForm()
)を持つクラスです。
_import 'package:flutter_web/cupertino.Dart';
import 'package:web_flutter/model/model.Dart';
import 'package:web_flutter/services/service.Dart';
class TenantViewModel with ChangeNotifier {
Tenant _tenant;
Property _property;
Service _service;
SubmissionResponse _submission;
ResponseData _responseData;
TenantViewModel(this._tenant, this._service);
bool get isNew => _tenant.id == null;
set tenant(Tenant tenant) {
if (_tenant != tenant) {
_tenant = tenant;
notifyListeners();
}
}
Tenant get tenant => _tenant;
set property(Property property) {
_tenant.propertyId = property.id;
notifyListeners();
}
Property get property => _property;
set submission(SubmissionResponse submission) {
if (_submission != submission) {
_submission = submission;
notifyListeners();
}
}
SubmissionResponse get submission => _submission;
set responseData(ResponseData responseData) {
if (_responseData != responseData) {
_responseData = responseData;
notifyListeners();
}
}
ResponseData get responseData => _responseData;
Future generateForm(String templateID) async {
SubmissionResponse submission;
submission = await _service.generateForm(_tenant.id, templateID);
this.submission = submission;
notifyListeners();
}
Future getSubmission(String submissionID) async {
ResponseData responseData;
responseData = await _service.getSubmission(submissionID);
this.responseData = responseData;
notifyListeners();
}
Future save() async {
Tenant updatedTenant;
if (tenant.isNew) {
updatedTenant = await _service.createTenant(_tenant);
this.tenant = updatedTenant;
} else {
updatedTenant = await _service.updateTenant(tenant);
this.tenant = updatedTenant;
}
notifyListeners();
}
Future refresh() async {
if (isNew) {
return;
}
var updatedTenant = await _service.getTenant(_tenant.id);
if (_tenant.propertyId != null) {
_property = await _service.getProperty(_tenant.propertyId);
}
_tenant = updatedTenant;
notifyListeners();
}
}
_
私が試した:テストファイルで_import 'package:flutter_test/flutter_test.Dart';
_を使用して、これをpubspec.yamlに追加します。
_ flutter_test:
sdk: flutter
_
FlutterではなくFlutter Webを使用しているため、これらは独自のさまざまなエラーを引き起こします。誰かが私が間違っているところを見ることができますか?
私が取り組んでいるテストは、おそらくそれ自体が間違っていることに気づいていますが、実行して失敗するまでには至りません。
私の技術リーダーが指摘したように、「ユニットテストはDart:html
にアクセスできません。ブラウザーで実行しない限り、」問題が発生します。
Pub.dev/packages/testから:
「デフォルトでは、テストはDart VM [仮想マシン]で実行されますが、pub run test -p chrome path/to/test.Dart
を渡すことでブラウザーでも実行できます。test
はブラウザの起動とテストのロードに注意してください。すべての結果は、VMテストの場合と同様にコマンドラインに報告されます。実際、両方のプラットフォームでテストを実行することもできます。単一のコマンド:pub run test -p "chrome,vm" path/to/test.Dart
"
私の特定の問題を解決するために、pub run test test/print_reports_card_test.Dart -p chrome
を使用してテストを実行しました。