Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Created August 21, 2012 07:01
Show Gist options
  • Select an option

  • Save a2ikm/3412907 to your computer and use it in GitHub Desktop.

Select an option

Save a2ikm/3412907 to your computer and use it in GitHub Desktop.
->のテスト、自分ならこう書く http://d.hatena.ne.jp/sinsoku/20120820/1345470914
# coding: utf-8
require "spec_helper"
describe User do
describe "#admin?" do
subject { user.admin? }
context "管理者の場合" do
let(:user) { User.new(role: "admin") }
it { should be_true }
end
context "一般ユーザの場合" do
let(:user) { User.new(role: nil) }
it { should be_false }
end
end
describe "#runnable_system?" do
subject { user.runnable_system? }
context "管理者がリンディさんの場合" do
let(:user) { User.new(name: 'Lindi') }
before { user.stub(admin?: true) }
it { should be_true }
end
end
end
@a2ikm

a2ikm commented Aug 21, 2012

Copy link
Copy Markdown
Author

書き換えた点は次の4つ。

  • spec_helperをrequireする
  • インスタンス変数じゃなくてletを使う
  • subjectにメソッド呼び出しも含める
  • #hoge?のテストにshould be_hogeは使わない(RSpecの成分が入ってなんか気持ち悪い)

ただ2つ目のテストはstubしちゃっていいのかわからない。
runnable_system?がadmin?依存なのかUserのname依存なのかわからないので。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment