Created
November 12, 2013 13:56
-
-
Save dblock/7431207 to your computer and use it in GitHub Desktop.
Working example for https://mail.google.com/mail/u/0/#label/_+Open+Source%2FGrape/14236a566557504a.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| require 'grape' | |
| module DATA | |
| class User | |
| attr_accessor :loginname, :email, :person | |
| def initialize(attrs) | |
| @loginname = attrs[:loginname] | |
| @email = attrs[:email] | |
| @person = attrs[:person] | |
| end | |
| end | |
| class Person | |
| attr_accessor :name, :avatar, :birthday | |
| def initialize(attrs) | |
| @name = attrs[:name] | |
| @avatar = attrs[:avatar] | |
| @birthday = attrs[:birthday] | |
| end | |
| end | |
| end | |
| module API | |
| class Person < Grape::Entity | |
| expose :name, :avatar | |
| end | |
| class User < Grape::Entity | |
| expose :loginname, :email | |
| expose :person, using: API::Person | |
| end | |
| end | |
| describe Grape::Entity do | |
| include Rack::Test::Methods | |
| subject do | |
| Class.new(Grape::API) do | |
| format :json | |
| get do | |
| person = DATA::Person.new(name: 'test', avatar: '/uri', birthday: '2013/11/11') | |
| user = DATA::User.new(loginname: 'test user', email: 'test@test.com', person: person) | |
| present user, with: API::User | |
| end | |
| end | |
| end | |
| def app | |
| subject | |
| end | |
| it "works" do | |
| get '/' | |
| last_response.status.should == 200 | |
| JSON.parse(last_response.body).should == {"loginname"=>"test user", "email"=>"test@test.com", "person"=>{"name"=>"test", "avatar"=>"/uri"}} | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment