开发者

What are some options for unit testing a RESTful API written for Rails or Sinatra?

I have a REST API written for Sinatra - I would like to build some good unit tests, and wanted the community's input on whic开发者_如何学Goh libraries I should spend my time investigating.


Have a look at Testing Sinatra with Rack::Test, rack-test itself and RSpec.


I'd go with boring old RSpec or Test::Unit. To get 80% of the test coverage with 20% of the effort, just test under your UI. 100% test coverage is not the goal, working software is. Your test verifies the "business rules" for app, the rest is lower risk UI stuff.

Suppose you're writing an application to estimate loan payments.

Your test would look something like this:

describe LoanCalculator do
  it "Estimates monthly payments given a loan amount, interest rate, and term" do
    LoanCalculator.new.estimate_payment(10000, 5, 48).should == 230.00
  end
end

Once you know your LoanCalculator works, you simply write your Sinatra app to delegate to it.

get '/loan_calculator' do
  @loan_amount = params[:amount]
  @rate = params[:rate]
  @term = params[:term]

  @result = LoanCalculator.new.estimate_payment(@loan_amount, @rate, @term)

  erb :loan_calculator_results
end

On a more complex system you would obviously have much more in your system under test, but keeping your UI thin and all of the logic within the SUT will get you huge gains without all of the hassle of more complex frameworks.

Hope that helps.

Brandon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜