spec/model/parse_spec.rb in her-0.5.5 vs spec/model/parse_spec.rb in her-0.6
- old
+ new
@@ -1,11 +1,11 @@
# encoding: utf-8
require File.join(File.dirname(__FILE__), "../spec_helper.rb")
describe Her::Model::Parse do
- context "when include_root_in_json is true" do
- context "when include_root_in_json is true" do
+ context "when include_root_in_json is set" do
+ context "to true" do
before do
spawn_model "Foo::User" do
include_root_in_json true
end
end
@@ -14,11 +14,11 @@
@new_user = Foo::User.new(:fullname => "Tobias Fünke")
@new_user.to_params.should == { :user => { :fullname => "Tobias Fünke" } }
end
end
- context "when include_root_in_json is set to another value" do
+ context "to a symbol" do
before do
spawn_model "Foo::User" do
include_root_in_json :person
end
end
@@ -27,11 +27,11 @@
@new_user = Foo::User.new(:fullname => "Tobias Fünke")
@new_user.to_params.should == { :person => { :fullname => "Tobias Fünke" } }
end
end
- context "when include_root_in_json is set in the parent class" do
+ context "in the parent class" do
before do
spawn_model("Foo::Model") { include_root_in_json true }
class User < Foo::Model; end
@spawned_models << :User
@@ -50,11 +50,11 @@
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end
end
- context "when parse_root_in_json is true" do
+ context "to true" do
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :user => { :id => 1, :fullname => "Lindsay Fünke" } }.to_json] }
stub.get("/users") { |env| [200, {}, [{ :user => { :id => 1, :fullname => "Lindsay Fünke" } }].to_json] }
stub.get("/users/1") { |env| [200, {}, { :user => { :id => 1, :fullname => "Lindsay Fünke" } }.to_json] }
@@ -85,11 +85,11 @@
@user.save
@user.fullname.should == "Tobias Fünke Jr."
end
end
- context "when parse_root_in_json is set to a symbol" do
+ context "to a symbol" do
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :person => { :id => 1, :fullname => "Lindsay Fünke" } }.to_json] }
end
@@ -100,11 +100,11 @@
@new_user = Foo::User.create(:fullname => "Lindsay Fünke")
@new_user.fullname.should == "Lindsay Fünke"
end
end
- context "when parse_root_in_json is set from the parent class" do
+ context "in the parent class" do
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :user => { :id => 1, :fullname => "Lindsay Fünke" } }.to_json] }
end
@@ -118,8 +118,37 @@
it "parse the data with the symbol" do
@new_user = User.create(:fullname => "Lindsay Fünke")
@new_user.fullname.should == "Lindsay Fünke"
end
+ end
+ end
+
+ context "when to_params is set" do
+ before do
+ Her::API.setup :url => "https://api.example.com" do |builder|
+ builder.use Her::Middleware::FirstLevelParseJSON
+ builder.use Faraday::Request::UrlEncoded
+ builder.adapter :test do |stub|
+ stub.post("/users") { |env| ok! :id => 1, :fullname => params(env)['fullname'] }
+ end
+ end
+
+ spawn_model "Foo::User" do
+ def to_params
+ { :fullname => "Lindsay Fünke" }
+ end
+ end
+ end
+
+ it "changes the request parameters for one-line resource creation" do
+ @user = Foo::User.create(:fullname => "Tobias Fünke")
+ @user.fullname.should == "Lindsay Fünke"
+ end
+
+ it "changes the request parameters for Model.new + #save" do
+ @user = Foo::User.new(:fullname => "Tobias Fünke")
+ @user.save
+ @user.fullname.should == "Lindsay Fünke"
end
end
end