Skip to content

Instantly share code, notes, and snippets.

@tonycoco
Last active May 25, 2023 06:04
Show Gist options
  • Save tonycoco/8798536 to your computer and use it in GitHub Desktop.
Save tonycoco/8798536 to your computer and use it in GitHub Desktop.

Revisions

  1. tonycoco revised this gist Aug 22, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions model_spec.rb
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,14 @@
    end

    context "validations" do
    it { should validate_presence_of(:example_attribute) }
    it { ensure_inclusion_of(:another_example_attribute).in_array(Resource::ExampleValues.constant_values).allow_blank }
    it { is_expected.to validate_presence_of(:example_attribute) }
    it { is_expected.to ensure_inclusion_of(:another_example_attribute).in_array(Resource::ExampleValues.constant_values).allow_blank }
    end

    context "associations" do
    it { should belong_to(:another_resource) }
    it { should have_one(:single_resource) }
    it { should have_many(:more_resources) }
    it { is_expected.to belong_to(:another_resource) }
    it { is_expected.to have_one(:single_resource) }
    it { is_expected.to have_many(:more_resources) }
    end

    context "scopes" do
  2. tonycoco revised this gist May 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion model_spec.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    describe Resource do
    it "should have a factory" do
    FactoryGirl.build(:resource).should be_valid
    expect(FactoryGirl.build(:resource)).to be_valid
    end

    context "constants" do
  3. tonycoco revised this gist Feb 4, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@
    let(:changes) { Faker::Lorem.words }

    before do
    patch :update, id: resource.id, resource: { example_atrribute: changes }
    patch :update, id: resource.id, resource: { example_attribute: changes }
    end

    it "responds successfully with an HTTP 204 status code" do
    @@ -59,7 +59,7 @@

    it "should have changed the resource's example attribute" do
    resource.reload
    expect(resource.example_atrribute).to eq(changes)
    expect(resource.example_attribute).to eq(changes)
    end
    end

  4. tonycoco revised this gist Feb 4, 2014. 2 changed files with 9 additions and 9 deletions.
    8 changes: 4 additions & 4 deletions controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -46,20 +46,20 @@

    context "PATCH #update" do
    let!(:resource) { FactoryGirl.create(:resource) }
    let(:changed_text) { Faker::Lorem.words }
    let(:changes) { Faker::Lorem.words }

    before do
    patch :update, id: resource.id, resource: { some_method: changed_text }
    patch :update, id: resource.id, resource: { example_atrribute: changes }
    end

    it "responds successfully with an HTTP 204 status code" do
    expect(response).to be_success
    expect(response.code).to eq("204")
    end

    it "should have changed the resource" do
    it "should have changed the resource's example attribute" do
    resource.reload
    expect(resource.some_method).to eq(changed_text)
    expect(resource.example_atrribute).to eq(changes)
    end
    end

    10 changes: 5 additions & 5 deletions model_spec.rb
    Original file line number Diff line number Diff line change
    @@ -12,20 +12,20 @@
    end

    context "validations" do
    it { should validate_presence_of(:some_method) }
    it { ensure_inclusion_of(:status).in_array(Resource::Statuses.constant_values).allow_blank }
    it { should validate_presence_of(:example_attribute) }
    it { ensure_inclusion_of(:another_example_attribute).in_array(Resource::ExampleValues.constant_values).allow_blank }
    end

    context "associations" do
    it { should belong_to(:user) }
    it { should have_one(:other_resource) }
    it { should belong_to(:another_resource) }
    it { should have_one(:single_resource) }
    it { should have_many(:more_resources) }
    end

    context "scopes" do
    describe "#self.scoped" do
    let!(:not_scoped) { FactoryGirl.create(:resource) }
    let!(:scoped) { FactoryGirl.create(:resource, scoped_method: true) }
    let!(:scoped) { FactoryGirl.create(:resource, attribute_for_scope: true) }

    it "should include only scoped resources" do
    expect(Resource.scoped).to include(scoped)
  5. tonycoco created this gist Feb 4, 2014.
    83 changes: 83 additions & 0 deletions controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    require "spec_helper"

    describe ExampleController do
    context "GET #index" do
    let(:resources) { FactoryGirl.create_list(:resource) }

    before do
    get :index
    end

    it "responds successfully with an HTTP 200 status code" do
    expect(response).to be_success
    expect(response.code).to eq("200")
    end

    it "loads all of the resources" do
    expect(assigns(:resources)).to match_array(resources)
    end
    end

    context "POST #create" do
    let(:resource_params) { FactoryGirl.attributes_for(:resource) }

    before do
    post :create, resource: resource_params
    end

    it "should respond successfully with an HTTP 201 status code" do
    expect(response).to be_success
    expect(response.code).to eq("201")
    end
    end

    context "GET #show" do
    let!(:resource) { FactoryGirl.create(:resource) }

    before do
    get :show, id: resource.id
    end

    it "responds successfully with an HTTP 200 status code" do
    expect(response).to be_success
    expect(response.code).to eq("200")
    end
    end

    context "PATCH #update" do
    let!(:resource) { FactoryGirl.create(:resource) }
    let(:changed_text) { Faker::Lorem.words }

    before do
    patch :update, id: resource.id, resource: { some_method: changed_text }
    end

    it "responds successfully with an HTTP 204 status code" do
    expect(response).to be_success
    expect(response.code).to eq("204")
    end

    it "should have changed the resource" do
    resource.reload
    expect(resource.some_method).to eq(changed_text)
    end
    end

    context "DELETE #destroy" do
    let!(:resource) { FactoryGirl.create(:resource) }
    let(:resource_id) { resource.id }

    before do
    delete :destroy, id: resource.id
    end

    it "responds successfully with an HTTP 204 status code" do
    expect(response).to be_success
    expect(response.code).to eq("204")
    end

    it "should destroy the resource" do
    expect { Resource.find(resource_id) }.to raise_error(ActiveRecord::RecordNotFound)
    end
    end
    end
    36 changes: 36 additions & 0 deletions model_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    require "spec_helper"

    describe Resource do
    it "should have a factory" do
    FactoryGirl.build(:resource).should be_valid
    end

    context "constants" do
    it "should create a range using the min and max" do
    expect(Resource::RANGE).to eq(Resource::MIN..Resource::MAX)
    end
    end

    context "validations" do
    it { should validate_presence_of(:some_method) }
    it { ensure_inclusion_of(:status).in_array(Resource::Statuses.constant_values).allow_blank }
    end

    context "associations" do
    it { should belong_to(:user) }
    it { should have_one(:other_resource) }
    it { should have_many(:more_resources) }
    end

    context "scopes" do
    describe "#self.scoped" do
    let!(:not_scoped) { FactoryGirl.create(:resource) }
    let!(:scoped) { FactoryGirl.create(:resource, scoped_method: true) }

    it "should include only scoped resources" do
    expect(Resource.scoped).to include(scoped)
    expect(Resource.scoped).to_not include(not_scoped)
    end
    end
    end
    end