DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on • Updated on

[RSpec] Controller/Request Spec Basic

prepare

set params to Post method

let(:attributes) {
  author: 'n350071',
  publisher: 'my publish'
}
subject { post :create, book: attributes }

set params to Get method

let(:params) {
  author: 'n350071'
}
subject { get :index, params }

set session

before { session[:book_id] = book.id }

Assertion

send_file: downnload

expect(response.headers['Content-Type']).to eq 'application/zip'
expect(response.headers['Content-Type']).to eq 'application/pdf'
expect(response.headers['Content-Type']).to eq 'application/doc'

status & render & redirect_to

expect(response.status).to eq 200
expect(response).to render_template :new

expect(response.status).to eq 302
expect(response).to redirect_to(new_book_path)

404 test

The 404.html is stored in 'public/404.html'.

describe '#show' do
  context 'the resource is not found' do
    it 'should render 404' do
      get :file_download
      expect(response.status).to eq 404
      expect(response).to render_template(file: Rails.root.join('public','404.html'))
    end
  end
end

JSON response

it 'should respond JSON and returns message Hello' do
  json_data = JSON.parse(response.body)
  expect(json_data['message']).to eq 'Hello'
end

πŸ”— Parent Note

Top comments (0)