Skip to main content

Home/ Larvata/ Group items tagged rspec

Rss Feed Group items tagged

張 旭

File: README - Documentation by YARD 0.8.7.6 - 0 views

  • we can express concepts like a conversation
    • 張 旭
       
      描述 order 這個東西。 order 就是將登記在它上面的物品價格加總起來。
  • The describe method creates an ExampleGroup.
  • ...15 more annotations...
  • declare examples using the it method
  • an example group is a class in which the block passed to describe is evaluated
  • The blocks passed to it are evaluated in the context of an instance of that class
  • nested groups using the describe or context methods
  • can declare example groups using either describe or context
  • can declare examples within a group using any of it, specify, or example
  • Declare a shared example group using shared_examples, and then include it in any group using include_examples.
  • Nearly anything that can be declared within an example group can be declared within a shared example group.
  • shared_context and include_context.
  • When a class is passed to describe, you can access it from an example using the described_class method
  • rspec-core stores a metadata hash with every example and group
  • Example groups are defined by a describe or context block, which is eagerly evaluated when the spec file is loaded
  • Examples -- typically defined by an it block -- and any other blocks with per-example semantics -- such as a before(:example) hook -- are evaluated in the context of an instance of the example group class to which the example belongs.
  • Examples are not executed when the spec file is loaded
  • run any examples until all spec files have been loaded
張 旭

Transactions - RSpec Rails - RSpec - Relish - 0 views

  • before(:all) hooks are invoked before the transaction is opened. You can use this to speed things up by creating data once before any example in a group is run
張 旭

How to Test Rails Models with RSpec - Semaphore - 0 views

  • Behaviour-driven Development (BDD) as a software development process is composed of multiple subtechniques.
  • Models can sometimes be full-blown objects with rich behaviour.
  • An alternative is to use the shoulda gem.
  • ...3 more annotations...
  • reflect_on_association
  • add the business logic
  • Covering Edge Cases
張 旭

Ruby on Rails 實戰聖經 | 自動化測試 - 0 views

  • 最小的測試粒度叫做Unit Test單元測試,會對個別的類別和方法測試結果如預期。再大一點的粒度稱作Integration Test整合測試,測試多個元件之間的互動正確。最大的粒度則是Acceptance Test驗收測試,從用戶觀點來測試整個軟體。
  • 單元測試,通常會由開發者自行負責測試,因為只有你自己清楚每個類別和方法的內部結構是怎麼設計的。
  • 哪來的時間做自動化測試呢?這個想法是相當短視和業餘的想法
  • ...18 more annotations...
  • 這其實是一種投資,如果是簡單的程式,也許你手動執行一次就寫對了,但是如果是複雜的程式,往往第一次不會寫對,你會浪費很多時間在檢查到底你寫的程式的正確性,而寫測試就可以大大的節省這些時間。更不用說你明天,下個禮拜或下個月需要再確認其他程式有沒有副作用影響的時候,你有一組測試程式可以大大節省手動檢查的時間。
  • 幾乎每種語言都有一套叫做xUnit測試框架的測試工具
  • 標準流程是 1. (Setup) 設定測試資料 2. (Exercise) 執行要測試的方法 3. (Verify) 檢查結果是否正確 4. (Teardown) 清理還原資料
  • RSpec是一套改良版的xUnit測試框架,非常風行於Rails社群
  • 個別的單元測試應該是獨立不會互相影響的
  • 一個it區塊,就是一個單元測試,裡面的expect方法會進行驗證。
  • RSpec裡,我們又把一個小單元測試叫做example
  • BDD(Behavior-driven development)測試框架,相較於TDD用test思維,測試程式的結果。BDD強調的是用spec思維,描述程式應該有什麼行為。
  • describe和context幫助你組織分類,都是可以任意套疊的。
  • 每個it就是一小段測試,在裡面我們會用expect(…).to來設定期望
  • let可以用來簡化上述的before用法,並且支援lazy evaluation和memoized,也就是有需要才初始,並且不同單元測試之間,只會初始化一次,可以增加測試執行效率
  • let!則會在測試一開始就先初始一次,而不是lazy evaluation。
  • 先列出來預計要寫的測試,或是暫時不要跑的測試
  • specify和example都是it方法的同義字。
  • 進階一點你可以自己寫Matcher
  • RSpec分成數種不同測試,分別是Model測試、Controller測試、View測試、Helper測試、Route和Request測試
  • Rails內建有Fixture功能可以建立假資料,方法是為每個Model使用一份YAML資料。
  • 記得確認每個測試案例之間的測試資料需要清除
張 旭

Better Specs { rspec guidelines with ruby } - 0 views

  • # when referring to an instance method's name
  • . (or ::) when referring to a class method's name
  • Be clear about what method you are describing.
  • ...9 more annotations...
  • When describing a context, start its description with "when" or "with".
  • is_expected.to respond_with
  • In isolated unit specs, you want each example to specify one (and only one) behavior.
  • Contexts are a powerful method to make your tests clear and well organized
  • not isolated
  • est valid, edge and invalid case.
  • new projects always use the expect syntax
  • On one line expectations or with implicit subject we should use is_expected.to
  • When you have to assign a variable instead of using a before block to create an instance variable, use let.
張 旭

ruby-grape/grape: An opinionated framework for creating REST-like APIs in Ruby. - 0 views

shared by 張 旭 on 17 Dec 16 - No Cached
  • Grape is a REST-like API framework for Ruby.
  • designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs
  • Grape APIs are Rack applications that are created by subclassing Grape::API
  • ...54 more annotations...
  • Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class
  • mount multiple API implementations inside another one
  • mount on a path, which is similar to using prefix inside the mounted API itself.
  • four strategies in which clients can reach your API's endpoints: :path, :header, :accept_version_header and :param
  • clients should pass the desired version as a request parameter, either in the URL query string or in the request body.
  • clients should pass the desired version in the HTTP Accept head
  • clients should pass the desired version in the UR
  • clients should pass the desired version in the HTTP Accept-Version header.
  • add a description to API methods and namespaces
  • Request parameters are available through the params hash object
  • Parameters are automatically populated from the request body on POST and PUT
  • route string parameters will have precedence.
  • Grape allows you to access only the parameters that have been declared by your params block
  • By default declared(params) includes parameters that have nil values
  • all valid types
  • type: File
  • JSON objects and arrays of objects are accepted equally
  • any class can be used as a type so long as an explicit coercion method is supplied
  • As a special case, variant-member-type collections may also be declared, by passing a Set or Array with more than one member to type
  • Parameters can be nested using group or by calling requires or optional with a block
  • relevant if another parameter is given
  • Parameters options can be grouped
  • allow_blank can be combined with both requires and optional
  • Parameters can be restricted to a specific set of values
  • Parameters can be restricted to match a specific regular expression
  • Never define mutually exclusive sets with any required params
  • Namespaces allow parameter definitions and apply to every method within the namespace
  • define a route parameter as a namespace using route_param
  • create custom validation that use request to validate the attribute
  • rescue a Grape::Exceptions::ValidationErrors and respond with a custom response or turn the response into well-formatted JSON for a JSON API that separates individual parameters and the corresponding error messages
  • custom validation messages
  • Request headers are available through the headers helper or from env in their original form
  • define requirements for your named route parameters using regular expressions on namespace or endpoint
  • route will match only if all requirements are met
  • mix in a module
  • define reusable params
  • using cookies method
  • a 201 for POST-Requests
  • 204 for DELETE-Requests
  • 200 status code for all other Requests
  • use status to query and set the actual HTTP Status Code
  • raising errors with error!
  • It is very crucial to define this endpoint at the very end of your API, as it literally accepts every request.
  • rescue_from will rescue the exceptions listed and all their subclasses.
  • Grape::API provides a logger method which by default will return an instance of the Logger class from Ruby's standard library.
  • Grape supports a range of ways to present your data
  • Grape has built-in Basic and Digest authentication (the given block is executed in the context of the current Endpoint).
  • Authentication applies to the current namespace and any children, but not parents.
  • Blocks can be executed before or after every API call, using before, after, before_validation and after_validation
  • Before and after callbacks execute in the following order
  • Grape by default anchors all request paths, which means that the request URL should match from start to end to match
  • The namespace method has a number of aliases, including: group, resource, resources, and segment. Use whichever reads the best for your API.
  • test a Grape API with RSpec by making HTTP requests and examining the response
  • POST JSON data and specify the correct content-type.
1 - 7 of 7
Showing 20 items per page