代码之家  ›  专栏  ›  技术社区  ›  anoop

带Pact.js的Angular6 mock REST api,错误:失败:实际交互与mock MockService的预期交互不匹配

  •  0
  • anoop  · 技术社区  · 6 年前

    契约.js ,但得到错误 Failed: Actual interactions do not match expected interactions for mock MockService.

    当我试图得到 http://localhost:1234/users (模拟的pact服务器api。)

    {"message":"No interaction found for GET /","interaction_diffs":[]}

    预期行为: 实际的Rest API调用被转移到模拟服务器,我应该得到模拟输出。(请参见下面代码中的expectedUser.),而不是实际的api结果。

    以下是我的代码:

    业力形态

       ...
        pact: [{
          cors: true,
          port: 1234,
          consumer: "ui",
          provider: "usersservice",
          host: '127.0.0.1',
          dir: "pacts/",
          spec: 2
        }],
        proxies: {
           // below is a fake rest API I'm trying to mock.
          'http://jsonplaceholder.typicode.com/users': 'http://localhost:1234/users'
        },
    ...
    

    import { TestBed } from '@angular/core/testing';
    import { HttpClientModule } from '@angular/common/http';
    import { UsersService } from './shared/users.service';
    import { User } from './shared/user';
    import { PactWeb, Matchers } from '@pact-foundation/pact-web';
    
    describe('UsersService pack test', () => {
        let provider;
    
        beforeAll(function (done) {
            provider = new PactWeb({
                cors: true,
                consumer: 'ui',
                provider: 'users-service',
                port: 1234,
                host: '127.0.0.1',
            });
            // required for slower CI environments
            setTimeout(done, 2000);
    
            // Required if run with `singleRun: false`
            provider.removeInteractions();
        });
    
        afterAll(function (done) {
            provider.finalize()
                .then(function () {
                    done();
                }, function (err) {
                    done.fail(err);
                });
        });
    
        beforeEach(() => {
            TestBed.configureTestingModule({
                imports: [
                    HttpClientModule
                ],
                providers: [
                    UsersService
                ],
            });
        });
    
        afterEach((done) => {
            provider.verify().then(done, e => done.fail(e));
        });
    
        describe('getUsers()', () => {
            const expectedUser: User[] = [{
                "id": 9,
                "name": "test user",
                "email": "test@user.xyz",
                "phone": "1-999-999-9999 x99999",
                }];
    
            beforeAll((done) => {
                provider.addInteraction({
                    state: `provider return a expected user array`,
                    uponReceiving: 'a request to GET a person',
                    withRequest: {
                        method: 'GET',
                        path: 'http://jsonplaceholder.typicode.com/users',
                    },
                    willRespondWith: {
                        status: 200,
                        body: expectedUser, // to respond with defined data.
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    }
                }).then(done, error => done.fail(error));
            });
    
            it('should get Person array', (done) => {
    
                const usersService: UsersService = TestBed.get(UsersService);
                usersService.getUsers().subscribe(response => {
                    console.log(response) // this logs actual API response not mocked.
                    expect(response).toEqual(expectedUser); // this fails
                    done();
                }, error => {
                    done.fail(error);
                });
            });
    
        });
     });
    

    我遵循了下面的源代码,但是无法创建并重定向到模拟的API

    https://reflectoring.io/consumer-driven-contracts-with-angular-and-pact/

    https://github.com/thombergs/code-examples/tree/master/pact/pact-angular

    1 回复  |  直到 6 年前
        1
  •  0
  •   Matthew Fellows    6 年前

    据我所知,在您的测试设置中,没有任何地方可以告诉您的代码它的API端点是不同的——您需要告诉http库/客户机去点击pact API而不是真正的API(pact不会自动劫持您的http客户机)。

    还有,你的 WithRequest 看来里面有条非法的路。不能重写协定协定中的主机名。