无用的错误:从`when()````''''
我正在使用 oilbtail 0.3.0 软件包来测试HTTPADAPTER类的请求()方法。此方法应调用 client 类的 http 软件包的post()方法。
class HttpAdapter {
static const headers = {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.acceptHeader: 'application/json',
};
final Client client;
HttpAdapter(this.client);
Future<void> request({
required String url,
required String method,
HttpClientBody? body,
}) async {
await client.post(Uri.parse(url), headers: HttpAdapter.headers);
}
}
class ClientSpy extends Mock implements Client {
void mockPost(String url) {
when(() => post(
Uri.parse(url),
headers: any(named: 'headers'),
)).thenAnswer(
(_) async => Response('{}', HttpStatus.ok),
);
}
}
当我使用下面的代码进行测试时,一切都很好:
void main() {
test('Should call post() with correct parameters', () async {
// Arrange
final client = ClientSpy();
final sut = HttpAdapter(client);
when(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
)).thenAnswer(
(_) async => Response('{}', HttpStatus.ok),
);
// Act
await sut.request(url: url, method: method);
// Assert
verify(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
));
});
}
但是,如果我替换了通过MockPost()方法替换While()指令,我会收到错误:不良状态:从'While()''内部调用任何方法存根。是一种真正的方法,还是一种扩展方法?
void main() {
test('Should call post() with correct parameters', () async {
// Arrange
final client = ClientSpy();
final sut = HttpAdapter(client);
client.mockPost();
// Act
await sut.request(url: url, method: method);
// Assert
verify(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
));
});
}
我在做什么错?
I'm using mocktail 0.3.0 package to test the request() method from HttpAdapter class. This method should call post() method of the Client class of the http package.
class HttpAdapter {
static const headers = {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.acceptHeader: 'application/json',
};
final Client client;
HttpAdapter(this.client);
Future<void> request({
required String url,
required String method,
HttpClientBody? body,
}) async {
await client.post(Uri.parse(url), headers: HttpAdapter.headers);
}
}
class ClientSpy extends Mock implements Client {
void mockPost(String url) {
when(() => post(
Uri.parse(url),
headers: any(named: 'headers'),
)).thenAnswer(
(_) async => Response('{}', HttpStatus.ok),
);
}
}
When I test using the code below, everything goes well:
void main() {
test('Should call post() with correct parameters', () async {
// Arrange
final client = ClientSpy();
final sut = HttpAdapter(client);
when(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
)).thenAnswer(
(_) async => Response('{}', HttpStatus.ok),
);
// Act
await sut.request(url: url, method: method);
// Assert
verify(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
));
});
}
But if I replace the when() instruction by mockPost() method I get the error: Bad state: No method stub was called from within 'when()'. Was a real method called, or perhaps an extension method?
void main() {
test('Should call post() with correct parameters', () async {
// Arrange
final client = ClientSpy();
final sut = HttpAdapter(client);
client.mockPost();
// Act
await sut.request(url: url, method: method);
// Assert
verify(() => client.post(
Uri.parse(url),
headers: HttpAdapter.headers,
));
});
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我弄清楚了这个问题。
Clientpy类从HTTP软件包实现客户端类。
客户端类有一个称为post()的方法,女巫是我想打电话的方法。
但是,HTTP软件包也具有post()函数(我不知道)。因此,我正在调用post()函数client.post()方法。
下面的代码现在正常工作:
或
I think I figured out the problem.
The ClientSpy class implements the Client class from http package.
The Client class has a method called post(), witch is the one I want to call.
However, http package has a post() function also (I did't know that). So, I was calling the post() function instead Client.post() method.
The code below is working fine now:
or