Permalink
Browse files

Add README section for testing models

  • Loading branch information...
1 parent 5582304 commit c4108fa3173fc920388d592746107f21339801c4 @remiprev committed May 13, 2012
Showing with 32 additions and 0 deletions.
  1. +32 −0 README.md
View
@@ -450,6 +450,38 @@ Her::API.setup :url => "https://api.example.com", :ssl => ssl_options do |builde
end
```
+## Testing
+
+Using Faraday stubbing feature, it’s very easy to write tests for our models. For example, using [RSpec](https://github.com/rspec/rspec-core):
+
+```ruby
+# app/models/post.rb
+class Post
+ include Her::Model
+ custom_get :popular
+end
+
+# spec/models/post.rb
+describe Post do
+ before do
+ Her::API.setup :url => "http://api.example.com" do |builder|
+ builder.use Her::Middleware::FirstLevelParseJSON
+ builder.use Faraday::Request::UrlEncoded
+ builder.adapter :test do |stub|
+ stub.get("/users/popular") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
+ end
+ end
+ end
+
+ describe ".popular" do
+ it "should fetch all popular posts" do
+ @posts = Post.popular
+ @posts.length.should == 2
+ end
+ end
+end
+```
+
## Things to be done
* Better error handling

0 comments on commit c4108fa

Please sign in to comment.