Rails 5 Test Prescriptions 第14章 Testing Exteranl Services(中断。)
- external testing strategy ✅
- the service integration test✅
- introduce VCR✅
- Client Unit Tests ❌
- Why an Adapter?
- Testing for Error Cases
- Smoke Tests and VCR Options 冒烟测试!🌿
- The world is a Service
使用test isolation 和 使用mock来限制测试的scope.可以帮助你成功测试一个额外的服务,并且可以断网。
External Testing Strategy
额外服务的测试故事包含2个主要角色:
- client:使用额外API的你的一部分程序代码。既因为他需要通过API来存取数据,也因为他发送给某人使用的data给API。每个案例,都会处理一个request和一个response
- server:在你的应用程序之外,通过一些类型的网络request可以到达这个服务。(本章使用的一些策略在没有断网的时候也可以使用)
- a fake server: 在一个测试和返回一个回复对象的期间,拦截HTTP request。使用VCR gem来关联fake server
- a adapter:是一个对象,用与client和server之间的调节。
- Smoke test: 从client到真实服务器的完整的交互的测试。client->adapter->Fake server->Server (完整)
- integration test:从client->adapter->Fake Server的测试。也是完成的测试,但不是真实的测试,因为使用了stubbed response来代替真实server。 这里会使用go-to 策略。
- Client Unit test: 从client到adapter结束。调节器的response是stubbed。调节器没有真正使用fake server调用。这个好处就是:让单元测试客户端完全和server API相隔离。
- a adapter unit test: 从adapter到Fake server。整体测试链条的中间。用于验证 调节器从客户端开始或者事件服务端的行为。
The Service Integration Test
增加三个gem
gem 'twitter' 和twitter交互的。4千+🌟
gem 'vcr' , group: :test 用于请求测试。 4千+🌟
gem 'webmock' , group: :test 用于模拟。2千+🌟
首先需要twitter API key和秘匙 。从它的程序管理页面https://apps.twitter.com/, 生成自己的应用程序的keys
建立关联:
task.rb是 belongs_to :user, optional: true #不会进行关联的存在验证,可以没有关联。
user.rb是:has_many :tasks, dependent: nullify #删除user,关联的tasks的user_id设置为空。
Introducing VCR (Fake server)
作者最喜欢的测试工具。concept is simple。当VCR生效时,会拦截第三方的HTTp request。默认设置,第一次请求正常通过,VCR会把response数据储存下来。当测试再运行时,VCR拦截请求然后使用之前储存的response数据,返回这个数据对象。
我的理解:使用VCRgem让第二次运行相同的集成测试无需再请求远程的服务器了。就是虚假的服务器端的响应,在离线的时候自然可以使用了。猜测,就是别人复查你的测试代码后再运行一次,无需等待很长时间。
不过,但改变了一个测试,还得手动让VCR从新记录response。 可以设置一个时间段,超出时间才会再次改变response的记录
VCR and RSpec
让任意一个it or describe块来使用一个VCR cassette,需要自己配置。
spec/support/vcr.rb
几段解释没看...minit 和 cucumber略过
Client Unit Tests
看不懂,暂听。