spec/lib/frenetic/resource_spec.rb in frenetic-0.0.3 vs spec/lib/frenetic/resource_spec.rb in frenetic-0.0.4

- old
+ new

@@ -1,22 +1,24 @@ describe Frenetic::Resource do - @client = Frenetic.new('url' => 'http://example.org') + let(:client) { Frenetic.new('url' => 'http://example.org') } - let(:resource) { Frenetic::Resource.new } + let(:resource) { described_class.new } + let(:description_stub) do Frenetic::HalJson::ResponseWrapper.new('resources' => { 'schema' => { 'resource' => { 'properties' => { 'foo' => 2 } } } } ) end subject { resource } context "created from a Hash" do - let(:resource) { Frenetic::Resource.new( foo: 'bar' ) } + let(:resource) { described_class.new( foo: 'bar' ) } it { should respond_to(:foo) } + its(:links) { should be_a Hash } its(:links) { should be_empty } end context "created from a HAL-JSON response" do let(:api_response) do @@ -29,35 +31,37 @@ } end let(:wrapped_response) do Frenetic::HalJson::ResponseWrapper.new(api_response) end - let(:resource_a) { Frenetic::Resource.new( wrapped_response ) } - let(:resource_b) { Frenetic::Resource.new } + let(:resource_a) { described_class.new( wrapped_response ) } + let(:resource_b) { described_class.new } before do - @client.stubs(:description).returns( description_stub ) + client.stub(:description).and_return description_stub - Frenetic::Resource.stubs(:api).returns( @client ) + described_class.stub(:api).and_return client - resource_a and resource_b + resource_a && resource_b end context "initialized with data" do subject { resource_a } its(:foo) { should == 1 } it { should_not respond_to(:bar) } its(:links) { should_not be_empty } end + context "intiailized in sequence without data" do subject { resource_b } it { should_not respond_to(:foo) } it { should_not respond_to(:bar) } its(:links) { should be_empty } end + context "with embedded resources" do let(:api_response) do { '_links' => { '_self' => { '_href' => 'bar' } @@ -75,42 +79,42 @@ end end describe ".api_client" do context "with a block" do - before { resource.class.api_client { @client } } + before { resource.class.api_client { client } } it "should reference the defined API client" do - subject.class.api.should == @client + subject.class.api.should == client end end context "with an argument" do - before { resource.class.api_client @client } + before { resource.class.api_client client } it "should reference the defined API client" do - subject.class.api.should == @client + subject.class.api.should == client end end end describe ".schema" do subject { resource.class.schema } context "with a defined API Client" do before do - @client.stubs(:description).returns( description_stub ) + client.stub(:description).and_return description_stub - resource.class.api_client @client + resource.class.api_client client end it "should return the schema for the specific resource" do subject.should == description_stub.resources.schema.resource.properties end end context "without a defined API Client" do - before { Frenetic::Resource.stubs(:respond_to?).with(:api).returns( false ) } + before { described_class.stub(:respond_to?).with(:api).and_return false } it "should raise an error" do expect { subject }.to raise_error(Frenetic::MissingAPIReference) end end \ No newline at end of file