それじゃあ、specを書こうか。(1)

さて。
script/generate rspec_model Roleなんぞとした暁には、spec/models/role_spec.rbなんぞというファイルが生成されている訳で候。


で、開いてみると、

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Role do
  before(:each) do
    @valid_attributes = {
    }
  end

  it "should create a new instance given valid attributes" do
    Role.create!(@valid_attributes)
  end
end


何度と記されている訳で候。


ん〜、じゃあ、とりあえず、あれ、だ。
@valid_attributesってことは、ここに適当と思われる値をセットすれば良いのだな? ん? ん?
という訳で、こんな感じにしてみた。

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Role do
  before(:each) do
    @valid_attributes = {
      :name => "Hogehoge",
      :description => "Fugafuga"
    }
  end

  it "should create a new instance given valid attributes" do
    Role.create!(@valid_attributes)
  end
end


で、回す。

$ script/spec -c spec/models/role_spec.rb
#=>.
#=>
#=>Finished in 0.066145 seconds
#=>
#=>1 example, 0 failures


...良いのじゃな? 良いのじゃな?


で、これで、まあ、とりあえず、新規作成することができることは、確認出来ました、と。
...で?


よく解らんので、とりあえず、restful_authenticationが作った、spec/models/user_spec.rbを参考に、書き換えよう。

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Role do
  fixtures :roles

  describe "being created" do
    before do
      @role = nil
      @creating_role = lambda do
        @role = create_role
        violated "#{@role.errors.full_message.to_sentence}" if @role.new_record?
      end
    end

    it "increments Role#count" do
      @creating_role.should change(Role, :count).by(1)
    end
  end

protected
def create_role(options = {})
  role = Role.new({
    :name => "Hogehoge",
    :description => "Fugafuga"
  }.merge(options))
  role.save if role.valid?
  role
end
end


良いのじゃな? 良いのじゃな?


じゃあ、検証出来るかどうか。か?

(略)
+  it "requires name" do
+    lambda do
+      role = create_role(:name => nil)
+      role.erros.on(:name).should_not be_nil
+    end
+  end
+
+  it "requires description" do
+    lambda do
+      role = create_role(:description => nil)
+      role.errors.on(:description).should_not be_nil
+    end
+  end
(略)


で、回す。

$ script/spec -c spec/models/role_spec.rb
#=>...
#=>
#=>Finished in 0.070318 seconds
#=>
#=>3 example, 0 failures


...はっ!? 何事じゃ?
いやいや。
validation、何も書いてないよ?
なんで、errorsに何かが入っているっぽいの?


ちょっと、悩むことにする。


スマン、解った。こうだ。

(略)
+  it "requires name" do
+    lambda do
+      role = create_role(:name => nil)
+      role.erros.on(:name).should_not be_nil
+    end.should_not change(Role, :count)
+  end
+
+  it "requires description" do
+    lambda do
+      role = create_role(:description => nil)
+      role.errors.on(:description).should_not be_nil
+    end.should_not change(Role, :count)
+  end
(略)


これで回す。

$ script/spec -c spec/models/role_spec.rb
#=>FF.
#=>
#=>1)
#=>'Role requires name' FAILED
#=>expected nil? to return false, got true
#=>./spec/models/role_spec.rb:23:
#=>./spec/models/role_spec.rb:21:
#=>script/spec:5:
#=>
#=>2)
#=>'Role requires description' FAILED
#=>expected nil? to return false, got true
#=>./spec/models/role_spec.rb:30:
#=>./spec/models/role_spec.rb:28:
#=>script/spec:5:
#=>
#=>Finished in 0.075339 seconds
#=>
#=>3 examples, 2 failures


おおっ!
ちゃんとfailure返ってきたっ!!


良きに計らえ、良きに計らえっ:-)


つうか、failure返ってきて喜ぶなんて、初めての経験www
なんて云うの?
$「私、他に好きな人ができちゃったの」
#=>「えっ!? そうなんだ(...ラッキーっ:-))」
みたいな?www


まあ、この後、failure返ってこないように四苦八苦するかどうかが、ビヘイビア駆動との違いですかwww


つうか、ね?
なんで、endの後ろに.should_not change(Role, :count)とかって書くの?
なんか、書き方そのもの理解ができねぇ...