Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add test helpers

  • Loading branch information...
commit b14fe35684277db3e14d7b8eeb4733e5e48d5c8a 1 parent cceaf07
@josepjaume josepjaume authored
View
8 lib/empresaula-api/config.rb
@@ -9,11 +9,11 @@ def self.config
end
class Config
- attr_reader :site
+ attr_reader :host
- def site=(site)
- @site = site
- Resource::Base.site = @site
+ def host=(host)
+ @host = host
+ Resource::Base.site = "http://#{@host}/api"
end
end
end
View
27 lib/empresaula-api/test_helpers/user.rb
@@ -0,0 +1,27 @@
+require 'empresaula-api/resource/user'
+require 'active_resource'
+require 'json'
+
+module Empresaula
+ module API
+ module TestHelpers
+ module User
+ def mock_current_user(user)
+ cookies = Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
+ cookies[:empresaula_session] = 'qwertyuiop123456789'
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/api/users/qwertyuiop123456789.json", {},
+ user.to_json
+ end
+ end
+
+ def mock_user(user)
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/api/users/#{user[:id]}.json", {},
+ user.to_json
+ end
+ end
+ end
+ end
+ end
+end
View
8 test/empresaula-api/config_test.rb
@@ -3,13 +3,13 @@
module Empresaula
describe "config" do
- describe "#site=" do
+ describe "#host=" do
it "sets the resource default site" do
API.config do |config|
- config.site = 'http://localhost:3000'
+ config.host = 'localhost:3000'
end
- API.config.site.must_equal 'http://localhost:3000'
- API::Resource::Base.site.to_s.must_equal 'http://localhost:3000'
+ API.config.host.must_equal 'localhost:3000'
+ API::Resource::Base.site.to_s.must_equal 'http://localhost:3000/api'
end
end
end
View
4 test/empresaula-api/resource/user_test.rb
@@ -4,12 +4,12 @@
module Empresaula::API::Resource
describe User do
before do
- User.site = "http://fakehost"
+ User.site = "http://fakehost/api"
end
describe ".find_by_session_id" do
@user = {user: {name: "James", id: 1}}
ActiveResource::HttpMock.respond_to do |mock|
- mock.get '/users/find_by_session_id.json?session_id=session_key', {},
+ mock.get '/api/users/find_by_session_id.json?session_id=session_key', {},
@user.to_json
end
it "builds a new user given a session id" do
View
22 test/empresaula-api/test_helpers/user_test.rb
@@ -0,0 +1,22 @@
+require_relative '../../test_helper'
+require 'empresaula-api/test_helpers/user'
+
+module Empresaula::API::TestHelpers
+ describe User do
+ subject do
+ Class.new do
+ include User
+ end.new
+ end
+ describe "#mock_user" do
+ before do
+ Empresaula::API::Resource::User.site = "http://fake/api"
+ end
+ it "mocks a particular user" do
+ subject.mock_user({id: 1, name: "James"})
+ user = Empresaula::API::Resource::User.find(1)
+ user.name.must_equal "James"
+ end
+ end
+ end
+end
View
1  test/test_helper.rb
@@ -1,4 +1,3 @@
require 'minitest/spec'
require 'minitest/autorun'
-
require 'turn'
Please sign in to comment.
Something went wrong with that request. Please try again.