te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How should I avoid stubbing properties with Sinon.js - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How should I avoid stubbing properties with Sinon.js - Stack Overflow

programmeradmin3浏览0评论

I've discovered that Sinon doesn't let you stub properties, only methods. I'm trying to figure out how to deal with/embrace this.

I have the following code:

var Player = {
  addPoints: function(points) {
    this.score += points;
  },
  score: 0
}

var Game = {
  setPlayers: function(players) {
    this.players = players;
  },
  over: function() {
    return this.players.some(function(player) {
      return player.score >= 100;
    });
  },
}

Here's a test I wrote:

describe("Game", function() {
  it("is over if a player has at least 100 points", function() {
    var game = Object.create(Game);
    player = Object.create(Player);
    game.setPlayers([player]);
    player.addPoints(100);
    game.over().should.be.true;
  });
});

It feels wrong to me to have to go in and call addPoints() on Player when I'm testing Game. My initial instinct was to stub Player.points, but I can't do so, because Sinon only stubs properties, not methods.

How should I be thinking about this?

I've discovered that Sinon doesn't let you stub properties, only methods. I'm trying to figure out how to deal with/embrace this.

I have the following code:

var Player = {
  addPoints: function(points) {
    this.score += points;
  },
  score: 0
}

var Game = {
  setPlayers: function(players) {
    this.players = players;
  },
  over: function() {
    return this.players.some(function(player) {
      return player.score >= 100;
    });
  },
}

Here's a test I wrote:

describe("Game", function() {
  it("is over if a player has at least 100 points", function() {
    var game = Object.create(Game);
    player = Object.create(Player);
    game.setPlayers([player]);
    player.addPoints(100);
    game.over().should.be.true;
  });
});

It feels wrong to me to have to go in and call addPoints() on Player when I'm testing Game. My initial instinct was to stub Player.points, but I can't do so, because Sinon only stubs properties, not methods.

How should I be thinking about this?

Share Improve this question edited Jan 29, 2016 at 15:30 Drew Gaynor 8,4725 gold badges41 silver badges53 bronze badges asked Aug 2, 2013 at 7:24 michaelmichael 2,9973 gold badges21 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

I emailed the SinonJS mailing list, and the author wrote back:

Exactly like you have done. Directly writing to the score property pletely defeats the purpose of the addPoints method, and makes your test tightly coupled to the implementation (thus making it brittle).

If you really do want to "stub" the property, here's how:

describe("Game", function() {
  it("is over if a player has at least 100 points", function() { 
    var game = Object.create(Game); 
    player = Object.create(Player); 
    game.setPlayers([player]); 
    player.score = 100; 
    game.over().should.be.true;
  });
});

It's a property on an instance - no point in being too clever about it. I still remend you don't do it this way though.

You don't have to stub game.points just set it. So you can create a simple mock for Game, which is just an object holding a spy for the setPlayer method. and then you can set score in the test to what ever you want.

describe("Game", function() {
  var game;

  before(function(){
    game = {addPoints: jasmine.createSpy()}
  })

  it("is over if a player has at least 100 points", function() { 
    game.score = 100;
    player = Object.create(Player);
    game.setPlayers([player]);
    game.over().should.be.true;
  });
});
发布评论

评论列表(0)

  1. 暂无评论