2013-04-14 53 views
0

我從具有JSON夾具的完整JSON數據集中提取字段的子集。更好的方法我能想到的是:有沒有更好的方法來測試這個RSpec的Ruby類?

require "spec_helper" 

    # API ref.: GET /repos/:owner/:repo 
    # http://developer.github.com/v3/repos/ 

    describe Elasticrepo::RepoSubset do 

    context "extract a subset of repository fields" do 
     let(:parsed) { Yajl::Parser.parse(fixture("repository.json").read) } 
     subject { Elasticrepo::RepoSubset.new(parsed) } 

     context "#id" do 
     its(:id) { should eq(2126244) } 
     end 
     context "#owner" do 
     its(:owner) { should eq("twitter") } 
     end 
     context "#name" do 
     its(:name) { should eq("bootstrap") } 
     end 
     context "#url" do 
     its(:url) { should eq("https://api.github.com/repos/twitter/bootstrap") } 
     end 
     context "#description" do 
     its(:description) { should eq("Sleek, intuitive, and powerful front-end framework for faster and easier web development.") } 
     end 
     context "#created_at" do 
     its(:created_at) { should eq("2011-07-29T21:19:00Z") } 
     end 
     context "#pushed_at" do 
     its(:pushed_at) { should eq("2013-04-13T03:56:36Z") } 
     end 
     context "#organization" do 
     its(:organization) { should eq("Organization") } 
     end 
     context "#full_name" do 
     its(:full_name) { should eq("twitter/bootstrap") } 
     end 
     context "#language" do 
     its(:language) { should eq("JavaScript") } 
     end 
     context "#updated_at" do 
     its(:updated_at) { should eq("2013-04-13T19:12:09Z") } 
     end 
    end 
    end 

,但我不知道是否有一個更好,更聰明,更清潔,或只是這樣做的更優雅的方式。第一類TDD是這樣的:

module Elasticrepo 
    class RepoSubset 
    attr_reader :id, :owner, :name, :url, :description, :created_at, :pushed_at, 
       :organization, :full_name, :language, :updated_at 
    def initialize(attributes) 
     @id = attributes["id"] 
     @owner = attributes["owner"]["login"] 
     @name = attributes["name"] 
     @url = attributes["url"] 
     @description = attributes["description"] 
     @created_at = attributes["created_at"] 
     @pushed_at = attributes["pushed_at"] 
     @organization = attributes["owner"]["type"] 
     @full_name = attributes["full_name"] 
     @language = attributes["language"] 
     @updated_at = attributes["updated_at"] 
    end 
    end 
end 

回答

2

我會刪除單個上下文塊。他們不添加任何額外的信息。

+0

在一天結束時......,我同意 –

1

我會使用鍵/值的映射並迭代,或創建一個具有正確值的對象並比較整個對象。

+0

我也喜歡你的方式:嘲諷一個假obj來比較 –

相關問題