首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mocha api测试路径

mocha api测试路径
EN

Stack Overflow用户
提问于 2016-03-13 22:25:40
回答 1查看 782关注 0票数 0

我在摩卡做了以下测试,产生了“未定义的AssertionError:未定义的==‘Ernest”。我隐约怀疑,测试实际上是在找到测试顶部创建的歌曲实例,我相信这证明了这一点。尽管如此,我不确定如何修复它。

这是为一个平均堆栈应用程序编写的api,其中猫鼬是ODM。

test.js

代码语言:javascript
运行
复制
it('can save a song', function(done) {
        Song.create({ title: 'saveASong' }, function(error, doc) {
            assert.ifError(error);
            var url = URL_ROOT + '/create/song/saveASong';
            superagent.
                put(url).
                send({
                    title: 'saveASong',
                    createdBy: 'Ernest'
                }).
                end(function(error, res) {
                    assert.ifError(error);
                    assert.equal(res.status, status.OK);
                    Song.findOne({}, function(error, song) {
                        assert.ifError(error);
                        assert.equal(song.title, 'saveASong');
                        assert.equal(song.createdBy, 'Ernest');
                        done();
                    });
                });
        });     
    });

我的路线:

代码语言:javascript
运行
复制
//PUT (save/update) song from the create view
    api.put('/create/song/:title', wagner.invoke(function(Song) {
        return function(req, res) {
            Song.findOne({ title: req.params.title}, function(error, song) {
                if(error) {
                    return res.
                        status(status.INTERNAL_SERVER_ERROR).
                        json({ error: error.toString() });
                }
                song.save(function(error, song) {
                    if(error) {
                        return res.
                            status(status.INTERNAL_SERVER_ERROR).
                            json({ error: error.toString() });
                    }
                    return res.json({ song: song });
                });
            });         
        };
    }));

更新:我在"end“之后添加了一个console.log(res.body),它不包括"createdBy: Ernest”k/v对。因此,我试图修改被发送到另一个k/v对(当然是模式)的对象,但是仍然没有任何东西持续存在。我不会收到任何错误,如果我注释掉“断言.相等.‘欧内斯特”行。

我的最新版本的PUT路由:

代码语言:javascript
运行
复制
api.put('/create/song/:title', wagner.invoke(function(Song) {
        return function(req, res) {
            Song.findOneAndUpdate({ title: req.params.title}, req.body ,{ new: true }, function(error, song) {

                if(error) {
                    return res.
                    status(status.INTERNAL_SERVER_ERROR).
                    json({ error: error.toString() });
                }

                return res.json({ song: song });
                });
        };          
    }));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 13:53:48

以下api路由

代码语言:javascript
运行
复制
api.put('/create/song/:title', wagner.invoke(function(Song) {
        return function(req, res) {
            Song.findOneAndUpdate({ title: req.params.title}, req.body ,{ new: true }, function(error, song) {

                if(error) {
                    return res.
                    status(status.INTERNAL_SERVER_ERROR).
                    json({ error: error.toString() });
                }

                return res.json({ song: song });
                });
        };          
    }));

通过以下摩卡测试

代码语言:javascript
运行
复制
it('can save a song', function(done) {
        Song.create({ title: 'saveASong' }, function(error, doc) {
            assert.ifError(error);
            var url = URL_ROOT + '/create/song/saveASong';
            superagent.
                put(url).
                send({
                    sections:[{ name: 'intro', timeSig: 140 }]
                }).
                end(function(error, res) {
                    assert.ifError(error);
                    assert.equal(res.status, status.OK);
                    console.log(res.body);
                    Song.findOne({}, function(error, song) {
                        assert.ifError(error);
                        assert.equal(song.title, 'saveASong');
                        //console.log(song);
                        assert.equal(song.sections[0].name, 'intro');
                        done();
                    });
                });
        });     
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35976646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档