ruby Rspec测试的最大命中:第1卷
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Rspec测试的最大命中:第1卷相关的知识,希望对你有一定的参考价值。
require "spec_helper"
describe Resource do
it "should have a factory" do
expect(FactoryGirl.build(:resource)).to 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 { 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 { 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
describe "#self.scoped" do
let!(:not_scoped) { FactoryGirl.create(:resource) }
let!(:scoped) { FactoryGirl.create(:resource, attribute_for_scope: 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
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(:changes) { Faker::Lorem.words }
before do
patch :update, id: resource.id, resource: { example_attribute: 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's example attribute" do
resource.reload
expect(resource.example_attribute).to eq(changes)
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
以上是关于ruby Rspec测试的最大命中:第1卷的主要内容,如果未能解决你的问题,请参考以下文章
ruby 1.9 和 RSpec2 有啥好的突变测试工具吗?
ruby [rspec]“使用RSpec测试Rails程序”笔记