如何用摩卡和西农存根测试发现护照策略?

时间:2019-12-19 10:21:33

标签: mocha passport.js chai sinon chai-http

我很难理解摩卡和西农存根的测试。我将Spotify Connect用于辅助项目,并且首先使用spotify-web-api-node模块,现在使用Spotify护照策略。但是对于这两个方面,我都在努力进行测试。我想在我的测试中对Spotify API的所有调用都存根。但是我不明白。

为了确保我覆盖护照对象的“身份验证”行为,我的第一个简单测试如下:

describe('Spotify Connection Process', function () {

  afterEach(() => {
    passport.authenticate.restore();
  });

  it('should redirect to the spotify url', function (done) {
    sinon.stub(passport, 'authenticate').returns('http://fake_spotify_url.com')
    chai.request(app)
      .get('/auth/spotify')
      .end(function(err, res) {
        return expect(res).to.redirectTo(passport.authenticate())
        done();
      })
  }) 
})

这是我的server.js文件,在其中使用passport.authenticate

app.get('/auth/spotify', 
  passport.authenticate('spotify', {
    scope: ['user-read-private', 'user-read-email', 'playlist-modify-private', 'playlist-modify-public'],
    showDialog: true
  }), 
  function(req, res) {
  // it'll redirect anyway
  }
);

passport.authenticate返回“真实”行为(如下所示),而不是我在测试文件中定义的行为。

Spotify Connection Process
    1) should redirect to the spotify url


  0 passing (504ms)
  1 failing

  1) Spotify Connection Process
       should redirect to the spotify url:
     Uncaught AssertionError: expected redirect to http://fake_spotify_url.com but got https://accounts.spotify.com/authorize?show_dialog=true&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fauth%2Fspotify%2Fcallback&scope=user-read-private%20user-read-email%20playlist-modify-private%20playlist-modify-public&client_id=baa975aeab4b41bcbbea7757a83c2f52 then https://accounts.spotify.com/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fscope%3Duser-read-private%2Buser-read-email%2Bplaylist-modify-private%2Bplaylist-modify-public%26response_type%3Dcode%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A8080%252Fauth%252Fspotify%252Fcallback%26show_dialog%3Dtrue%26client_id%

你知道为什么吗?我肯定会丢失一些东西!而且,从更广泛的角度来看,如何测试Spotify登录过程是否正常?

0 个答案:

没有答案