Ruby on Rails Tutorial (35)
ということで spec 配下の確認をしてみます。とりあえず spec/models/relationship_spec.rb から。こないだ試しに spec なナニを作ったのですが作法が分からず、でしたし。
ということで確認開始。先頭部分から。
require 'spec_helper' describe Relationship do let(:follower) { FactoryGirl.create(:user) } let(:followed) { FactoryGirl.create(:user) } let(:relationship) { follower.relationships.build(followed_id: followed.id) } subject { relationship } it { should be_valid }
ええと、subject というメソド呼び出し (?) で should のレシーバを略することができる、とありますね。最初の試験は、_relationship は valid であること_というカンジなんですかね。
ちなみに relationship は let で云々されてて、
follower.relationships.build(followed_id: followed.id)
follower が followed を follow (変なカンジ) してる状態、になるのかな。最初の試験は rspec てきにつなげて書いてみると
Relationship relationship should be_valid
なのかどうか。あるいは次の試験は
Relationship accessible attributes should not allow access to follower_id
次の試験は以下ですね (上のヤツは略)。
describe "follower methods" do it { should respond_to(:follower) } it { should respond_to(:followed) } its(:follower) { should == follower } its(:followed) { should == followed } end
こちらは
Relationship follower methods
について
- relationship が follower という属性を持っている
- relationship が followed という属性を持っている
- relationship.follower は follower と同値
- relationship.followed は followed と同値
ということを確認してますね。成程。でもちょっと文章としては微妙なカンジ?
次の試験は以下な記述で
describe "when followed id is not present" do before { relationship.followed_id = nil } it { should_not be_valid } end
ええと
Relationship when followed id is not present
な場合に relationship should not be valid なこと確認しとるのか。ここは以下な記述の試験になる訳ですね。
validates :follower_id, presence: true
次のも同様なので確認してる記述のみ引用。
Relationship when follower id is not present
次
spec/models/micropost_spec.rb 確認。
require 'spec_helper' describe Micropost do let(:user) { FactoryGirl.create(:user) } before { @micropost = user.microposts.build(content: "Lerem ipsum") } subject { @micropost } it { should respond_to(:content) } it { should respond_to(:user_id) } it { should respond_to(:user) } its(:user) { should == user } it {should be_valid }
user なオブジェクトを作っておいて @micropost なナニは毎回 before で生成。belongs_to な記述があるものについてはその先なオブジェクトから build するのが作法というか、生成しようが無いのか。
つうか、手元の実装は
class Micropost < ActiveRecord::Base attr_accessible :content belongs_to :user
ってなってて user_id は respond_to な状態ではないのだけれどこれ、どうなってんの。試験実行してみたんですが通るな。
これ、belongs_to :user ってなってるとその属性 (user_id) は accessible になるのかな。とりあえず根拠は無いけどそーなのだろう、という理解で先を見ます。つうか belongs_to で親を手繰れるはずなので公開されてる状態、って理解で良いのか。
む、この状態だと
class Micropost < ActiveRecord::Base attr_accessible :content belongs_to :user
user_id な属性にはアクセスできるけれど
it { should respond_to(:user_id) }
全ての意味で accessible ではない、のか。
describe "accessible attributes" do it "should not allow access to user_id" do expect do Micropost.new(user_id: user.id) end.to raise_error(ActiveModel::MassAssignmentSecurity::Error) end end
belongs_to なソレは
before { @micropost = user.microposts.build(content: "Lerem ipsum") }
という方法でオブジェクトが生成されるべき、という理解で良いのかな。
あら、同じ試験がありますね。面倒なので放置しておきます。試験的には
- attr_accessible な確認
- belongs_to な確認
- validates な確認
という部分のみ、になっているのか。
とりあえず別件対応
継続としては user_spec.rb 確認して controllers 方面とかに去る方向で。