Skip to content

Instantly share code, notes, and snippets.

@sebabelmar
Created October 8, 2015 15:02
Show Gist options
  • Save sebabelmar/a4210234d140de004fe1 to your computer and use it in GitHub Desktop.
Save sebabelmar/a4210234d140de004fe1 to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe TagsetsController, type: :controller do
let(:json) { JSON.parse(response.body) }
# This should return the minimal set of attributes required to create a valid
# Tagset. As you add validations to Tagset, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
{name: "First Tagset", description: "Test Tagset", _id: "FIRST"}
}
let(:valid_attributes_2) {
{name: "Second Tagset", description: "Test Tagset", _id: "SECND"}
}
let(:invalid_attributes) {
{name: "Second Tagset", description: "Test Tagset", _id: ""}
}
let(:tagset) {
Tagset.find("FIRST")
}
let(:tagset_2) {
Tagset.find("SECND")
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# TagsetsController. Be sure to keep this updated too. VALID SESSION FOR NOW
let(:valid_session) { {} }
describe "GET #index" do
before do
Tagset.create!(valid_attributes)
get :index, format: :json
end
it 'returns the tagsets' do
expect(json['data'][0]['name']).to eq(tagset.name)
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Tagset" do
expect {
post :create, {:tagset => valid_attributes}, valid_session
}.to change(Tagset, :count).by(1)
end
end
context "with invalid params" do
it "assigns a newly created but unsaved tagset as @tagset" do
post :create, {:tagset => invalid_attributes}, valid_session
expect(assigns(:tagset)).to be_a_new(Tagset)
end
end
end
describe "GET #validate" do
context "single id and array of id's" do
before do
Tagset.create!(valid_attributes)
Tagset.create!(valid_attributes_2)
end
it "response true for a existing valid set of id's" do
get :validate, {:tagsets_collection => [tagset.id, tagset_2.id]}, valid_session
expect([json['data']['FIRST'], json['data']['SECND']] ).to eq(['true', 'true'])
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model and entitlement")
}
it "updates the requested tagset when having entitlement" do
skip("Waiting for logic on controller")
end
it "do not update the requested tagset when missing entitlement" do
skip("Waiting for logic on controller")
end
end
end
describe "DELETE #delete" do
it "destroys the requested tagset" do
skip("Waiting for logic in controller")
end
end
end
#REQUESTS
require 'rails_helper'
RSpec.describe "Tagsets", type: :request do
let(:valid_attributes) {
{name: "New Tagset", description: "Test Tagset", _id: "TAGSE"}
}
let(:invalid_attributes) {
{name: "Second Tagset", description: "Test Tagset", _id: ""}
}
describe "GET /v1/tagsets" do
it "list Tagset request" do
get '/v1/tagsets'
expect(response).to have_http_status(200)
end
end
describe "POST /v1/tagsets" do
context "create Tagset request" do
it "succesful" do
post '/v1/tagsets', {:tagset => valid_attributes}
expect(response).to have_http_status(200)
end
it "unsuccesful" do
post '/v1/tagsets', {:tagset => invalid_attributes}
expect(response).to have_http_status(422)
end
end
end
describe "GET /v1/tagsets/validate" do
context "validate Tagset request" do
it "succesful" do
get '/v1/tagsets/validate?tagsets_collection[]=TAGSE'
expect(response).to have_http_status(200)
end
it "unsuccesful" do
get '/v1/tagsets/validate?tagsets[]=TAGSE'
expect(response).to have_http_status(422)
end
end
end
end
#ROUTES
require "rails_helper"
RSpec.describe TagsetsController, type: :routing do
let(:valid_attributes) {
{name: "New Tagset", description: "Test Tagset", _id: "TAGSE"}
}
describe "routing" do
before do
Tagset.create!(valid_attributes)
end
it "routes to #index" do
expect(:get => "/v1/tagsets").to route_to("tagsets#index")
end
it "routes to #create" do
expect(:post => "/v1/tagsets").to route_to("tagsets#create")
end
it "routes to #validate" do
expect(:get => "/v1/tagsets/validate").to route_to("tagsets#validate")
end
it "routes to #update" do
skip('route not working')
expect(:put => "/v1/tagsets/1").to route_to("tagsets#update", :id => "1")
end
it "routes to #delete" do
skip('route not working')
expect(:delete => "/v1/tagsets/1/delete").to route_to("tagsets#delete", :id => "1")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment