rspec-railsのドキュメント読み (2)

2013-06-16T00:00:00+00:00 rspec Ruby Rails

読みつつ検証していない所とかまだまだあるので先に進める。モデルのトランザクションな件は前回で書いたので(ry

routing spec

spec/routingディレクトリを作って、そこにテスト定義するかdescribeのtypeで:routingを指定する事でルーティングのテストが出来る模様

App::Application.routes.draw do
  resources :entry do
    collection do
    match "search/:query", :action => :search,
  end

  root :to => "entry#index"
end

的な感じなルーティングになってたとして、spec/routing/entry_routing_spec.rbを書く

require "spec_helper"

# spec/routingで処理させない場合は:type => :routingを指定する
# describe "routes for Entry", :type => :routing do

describe "route for Entry" do
  it "route to /" do
    expect(:get => "/").to be_routable
    expect(get("/")).to route_to("entry#index") # getっていうショートカットもある

    # もしcontrollerがnamespace内にあれば[ネームスペース名]/コントローラーで指定
  end

  it "route to /entry" do
    expect(:get => "/entry").to be_routable
    expect(:get => "/entry").to route_to("entry#index")

    # route_toは以下な感じでも書ける
    # expect(:get => "/entry").to route_to(:controller => "entry", :action => "index")
  end

  it "route to /entry/delete" do
    expect(:delete => "/entry").to be_routable
    expect(:delete => "/entry").to route_to("entry#destroy")
    expect(:post => "/entry").not_to route_to("entry#destroy")
  end

  it "route to /entry/search" do
    expect(:get => "/entry/search/A").to be_routable
    expect(:get => "/entry/search/A").to route_to("entry#search", :query => "A")

=begin
    expect(:get => "/entry/search/A").to route_to(
      :controller => "entry",
      :action => "search",
      :query => "A"
    )
=end

  end
end

モデルのerrors_on

モデルのvalidator辺りを検証するっぽい

require "spec_helper"

describe Sample do
  it "validates_presence_of :name" do
    sample = Sample.new
    expect(sample).to have(1).error_on(:name)

    I18n.locale = :en
    expect(sample.errors_on(:name)).to include("can"t be blank")

    I18n.locale = :ja
    expect(sample.errors_on(:name)).to include("を入力してください")
  end
end

まだバリデーターな辺りとかも勉強進んでないので微妙なんですが、日本語ロケールだとかで検証したい時とかはI18n.localeで書き換えちゃえば良い模様。ただafter辺りとかで初期状態に戻したりしておくべきなのではって思ったり

とりあえずは以上。あとはまだMailerとか辺り勉強してないので、それ次第で先に進める。バリデーターに関するやつも、もっと詳しくなってから色々やってみる

Jenkinsのジョブをcurlで処理する robotium(+ActionBarSherlock)を使ってみた