无用的错误:从`when()````''''

发布于 2025-02-09 04:43:46 字数 2044 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2025-02-16 04:43:46

我想我弄清楚了这个问题。

Clientpy类从HTTP软件包实现客户端类。
客户端类有一个称为post()的方法,女巫是我想打电话的方法。
但是,HTTP软件包也具有post()函数(我不知道)。因此,我正在调用post()函数client.post()方法。

下面的代码现在正常工作:

class ClientSpy extends Mock implements Client {
  void mockPost(String url) {
    when(() => this.post(
          Uri.parse(url),
          headers: any(named: 'headers'),
        )).thenAnswer(
      (_) async => Response('{}', HttpStatus.ok),
    );
  }
}

import 'package:http/http.dart' as http;

class ClientSpy extends Mock implements http.Client {
  void mockPost(String url) {
    when(() => post(
          Uri.parse(url),
          headers: any(named: 'headers'),
        )).thenAnswer(
      (_) async => Response('{}', HttpStatus.ok),
    );
  }
}

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:

class ClientSpy extends Mock implements Client {
  void mockPost(String url) {
    when(() => this.post(
          Uri.parse(url),
          headers: any(named: 'headers'),
        )).thenAnswer(
      (_) async => Response('{}', HttpStatus.ok),
    );
  }
}

or

import 'package:http/http.dart' as http;

class ClientSpy extends Mock implements http.Client {
  void mockPost(String url) {
    when(() => post(
          Uri.parse(url),
          headers: any(named: 'headers'),
        )).thenAnswer(
      (_) async => Response('{}', HttpStatus.ok),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文