9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpecでformat: jsonなアクションをテストする

Posted at

環境

  • rails: 4.0.9
  • rspec: 3.1.0

controller

formatでrenderを切り替えている

def index
  # 何がしかのjsonデータ
  @json = Json.all
  respond_to do |format|
    format.html
    format.json { render json: @json }
  end
end

RSpec

こうすると@jsonを返す方をテストできる

before do
  request.env["HTTP_ACCEPT"] = 'application/json'
  get :simulate, format: :json
end

subject { response.body }
it {
  is_expected.to be_json_eql(%({id: 'hoge', name: 'fuga'}))
}

どうにもRSpecを調べるのは骨が折れますね...

9
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?