Comments (12)
Hey @jerem17, I've got a solution for you.
Try adding TestWidgetsFlutterBinding.ensureInitialized();
in setUpAll hook function.
Cookies won't work anyway, because it saved to SharedPreferences which doesn't exist at test time.
If you rely on them, you might open an issue there.
from dart_nock.
Hi!
Sorry for the late reply.
Seems passing strings instead of matchers is working as expected.
I mean DioError [DioErrorType.RESPONSE]: Http status error [404]
is arising.
I was rushing forward while implementing URL matching, maybe should do some fixes to match the expected behavior.
Thanks for the issue. That helps a lot.
from dart_nock.
Hey @pblinux!
I've updated the package recently to support that use case. Just use version 1.1.2.
Thanks for your contribution!
from dart_nock.
Hello @cah4a ,
first of all thank you for the great work!
On the other hand, I have the same problem, I do 2 nock (url) .get ("/ example1) and another nock (url) .get (" / example2). The first goes well, but the second timeout ... I put "persist" to try but that doesn't change anything ...
nock("https://example.com/1").get("")
..reply(
200,
"ahaha",
);
nock("https://example.com/2").get("")
..reply(
200,
"ohoho",
);
from dart_nock.
Hey @jerem17,
Thanks for the kind words!
Just tried sequential scenario and it seems working:
import 'package:test/test.dart';
import 'package:nock/nock.dart';
import 'package:http/http.dart' as http;
void main() {
setUpAll(() {
nock.init();
nock.defaultBase = "http://example.com";
});
setUp(() {
nock.cleanAll();
});
test('sequntal_requests', () async {
final interceptor = nock.get("/users")
..reply(
200,
"result1",
);
final interceptor2 = nock.get("/users")
..reply(
200,
"result2",
);
final request1 = await http.get("http://example.com/users");
final request2 = await http.get("http://example.com/users");
expect(request1.body, "result1");
expect(request2.body, "result2");
expect(interceptor.isDone, true);
expect(interceptor2.isDone, true);
});
name: sequential_scenario_dart_nock_test
dev_dependencies:
test: ^1.15.7
nock: ^1.1.2
$ dart --version
Dart SDK version: 2.10.4 (stable) (Wed Nov 11 13:35:58 2020 +0100) on "macos_x64"
If that doesn't help feel free to open a new issue, with more detailed information.
from dart_nock.
@cah4a Thank you for your quick feedback! This changes nothing, I think the problem is that I am calling an external function. It looks like this:
my test.dart :
test('test1', () async {
final interceptor = nock.get("/users1")
..reply(
200,
"result1",
);
final interceptor2 = nock.get("/users2")
..reply(
200,
"result2",
);
expect(() async => auth.signIn("us1", "us2"), throwsException);
}, timeout: Timeout(Duration(seconds: 10)));
my external function, which is in a class I called "Auth" and which uses the "requests" package for http requests;
class Auth {
signIn(String us1, String us2) {
await Requests.get("http://example.com/users1", headers: {});
print("step 1");
await Requests.get("http://example.com/users2", headers: {});
print("step 2" );
}
}
The first request is to intercept, but not the second: /
Dart SDK version: 2.12.0-133.2.beta (beta) (Tue Dec 15 09:55:09 2020 +0100) on "macos_x64"
dev_dependencies:
test: ^1.15.7
nock: ^1.1.2
from dart_nock.
Should your code throw an exception?
import 'package:test/test.dart';
import 'package:nock/nock.dart';
import 'package:http/http.dart' as http;
Future<void> extern_function() async {
await http.get("http://example.com/users1");
await http.get("http://example.com/users2");
}
void main() {
setUpAll(() {
nock.init();
nock.defaultBase = "http://example.com";
});
setUp(() {
nock.cleanAll();
});
test('sequntal_requests', () async {
nock.get("/users1")
..reply(
200,
"result1",
);
nock.get("/users2")
..reply(
200,
"result2",
);
// TEST NOT PASS:
// expect(() async => extern_function(), throwsException);
// TEST PASS:
expect(extern_function(), completes);
});
}
from dart_nock.
@cah4a So I got some good news for you and some bad news for me lol
When I test your code using the http package, it works perfectly, however, when I use the requests package, it doesn't work anymore.
func :
Future<void> extern_function() async {
print("1");
await Requests.get("http://example.com/users1");
print("2");
await Requests.get("http://example.com/users2");
print("3");
}
result :
1
2
TimeoutException after 0:00:10.000000: Test timed out after 10 seconds.
dart:isolate _RawReceivePortImpl._handleMessage
So I guess I have to look at the "request" package side to find the solution to my problem
from dart_nock.
Hmm, that's pretty interesting.
Not that good news, btw. Nock has to support any library out there.
from dart_nock.
in addition I use the requests package in all my application lol
rip
Anyway thank you very much for taking the time to answer me and help me
from dart_nock.
You are a genius! Thank you very much, it works now.
from dart_nock.
@jerem17
My pleasure!
from dart_nock.
Related Issues (14)
- can't test HOT 2
- Question: is it possible to not mock certain URLs? HOT 3
- matching uri with path-params HOT 1
- POST requests with body aren't working HOT 1
- Synchronised head methods HOT 4
- Broken with Flutter 3 HOT 4
- Missing HttpClient methods since [email protected] HOT 1
- Response body mocking HOT 3
- Coverage HOT 9
- Post request not correctly mocked HOT 7
- Null safety support HOT 4
- Impossible to write POST interceptor HOT 1
- Recording support? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart_nock.