ruby-on-rails - 具有各种集成的 Rails 测试

标签 ruby-on-rails api testing salesforce

我有一个 Rails 应用程序,它与依赖于位于远程数据库中的外部数据的外部 API (Salesforce) 交互。我写了一个包装器来包装这段代码,这样用户就可以调用 get_by_id(id) 而不是编写相应的 sql 查询。

我想测试这段代码,但我不确定该如何着手。我应该访问 Salesforce 后端数据库进行测试,调用真正的方法吗?或者我应该只模拟方法调用的结果吗?我一直对我应该测试什么感到困惑......

最佳答案

您应该为 Salesforce 的交互编写一个套件。

测试的一个基本原则是,您的测试不应因外部因素而失败。但是,您的应用应该能够从 SalesForce 的错误中恢复。

来自 Rails 4 Test Prescriptions

Unfortunately, interacting with a third-party web service introduces a lot of complexity to our testing. Connecting to a web service is slow—even slower than the database connections we’ve already tried to avoid. Plus, connection to a web service requires an Internet connection... Some external services are public—we don’t want to post an update to Twitter every time we run our tests, let alone post a credit-card payment to PayPal.

另外,这本书有一些指南,

A fake server, which intercepts HTTP requests during a test and returns a canned response object. We’ll be using the VCR gem ...* An adapter, which is an object that sits between the client and the server to mediate access between them.

A smoke test, which goes from the client all the way to the real server...a full end-to-end test of the entire interaction. We don’t want to do this often, for all the reasons listed earlier, but it’s useful to be able to guard against changes in the server API.

An integration test, which goes from the client to the fake server. This tests the entire end-to-end functionality of our application but uses a stubbed response from the server.

A client unit test, which starts on the client and ends in the adapter. The adapter’s responses are stubbed, meaning that the adapter isn’t even making fake server calls. This allows us to unit-test our client completely separate from the server API.

An adapter unit test, which starts in the adapter and ends in the fake server. These tests are the final piece of the chain and allow us to validate the behavior of the adapter separate from any client or the actual server

顺便说一句,我认为这本书是必备的

关于ruby-on-rails - 具有各种集成的 Rails 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43784741/

相关文章:

ruby-on-rails - Rails 'public' 文件夹中文件的自定义内容类型

ruby-on-rails - Rails复选框AJAX调用,不想呈现任何内容

ios - IOS使用FBSDKLoginManager的安全问题

java - ArrayList(集合<?扩展E> c)

api - "private language WTF"与糟糕的库/API/DSL 设计有何区别?

java - 重载方法的 Groovy 单元测试

exception - 试图组合一些断言函数,但我无法尝试工作

ruby-on-rails - 多次创建调用 before_save

ruby-on-rails - Rails 合并 2 个查询结果

ios - 断言日期时测试失败