前天写了文章:
有webservice参与的系统的单元测试,最好使用mock object
如果某个mock对象,要求模拟 POST 这样的修改数据的操作,而不是简单的GET 这样的查询,该如何做呢?
我现在使用的办法,是 使用yaml文件来存储数据,达到简单的模仿 数据库的目的。
例如:
require 'yaml'
module YamlStoreStrategy
YAML_FILE_NAME = "spec/mock_attributes.yaml"
private
def update_yaml(hash)
content = YAML.load(File.open(YAML_FILE_NAME))
content[self.class.name] = hash
File.open(YAML_FILE_NAME, 'w') { |file| file.write(content.to_yaml)}
end
def result_hash_from_yaml
content = YAML.load(File.open(YAML_FILE_NAME))[self.class.name]
return content
end
end
require 'spec_helper'
class SomeMockResource
include YamlStoreStrategy
def run_private_methods_from_module
update_yaml("blablabla" => "foo")
result_hash_from_yaml
end
end
describe SomeMockResource do
before do
@some_mock_resource = SomeMockResource.new
end
it "should run the private methods from module" do
@some_mock_resource.run_private_methods_from_module
end
it "should update_yaml , then query from yaml" do
purpose = "test if the module works"
@some_mock_resource.send(:update_yaml,{"name"=>"some resource", "purpose"=> purpose})
@some_mock_resource.send(:result_hash_from_yaml)["purpose"].should == purpose
end
end
SomeMockResource:
那么,我们就可以在MockObject中引用这个 module:
require 'spec/support/yaml_store_strategy.rb'
class MockServerSettingResource < ServerSettingResource
include YamlStoreStrategy
def find(params)
return [result_hash_from_yaml.merge(params)]
end
def create(params)
updated_hash = result_hash_from_yaml.merge(params)
update_yaml(updated_hash)
return [updated_hash]
end
end
对该 Mock Object的测试:
require 'spec_helper'
describe MockServerSettingResource do
describe "create , then query" do
it "should create settings "do
key_name = "foo"
value = "value of the key: foo"
resource = MockServerSettingResource.new
resource.create({ :name => key_name, :value => value })
result = resource.find({:name => key_name})
result[:name].should == key_name
result[:value].should == value
end
end
end
分享到:
相关推荐
7. **自动化测试**:为了提高效率和减少人为错误,通常会将接口测试自动化,例如使用JUnit、TestNG等框架结合Mock服务器进行集成测试。 8. **持续集成**:将接口测试集成到持续集成/持续部署(CI/CD)流程中,每次...
在IT行业中,Web服务是应用程序之间进行通信的一种标准方式,而SOAP(Simple Object Access Protocol)是一种基于XML的协议,用于交换结构化的、机器可读的数据。SOAP WebService调试工具是开发人员用来测试、验证和...
使用Spring.NET,我们可以轻松地创建模拟对象(Mock Objects)替代真实的Web服务,以便在测试环境中进行单元测试。 8. **灵活性**: Spring.NET允许我们根据需求选择不同的Web服务协议(如SOAP、RESTful等),...
建议使用清晰、有意义的名字来命名测试用例、测试步骤等对象。 ##### 6.4 共享操作管理 - SVN 对于团队协作的项目,使用版本控制系统(如SVN)来管理测试项目的变更非常必要。SoapUI支持与SVN等版本控制系统的集成...
- **共享操作管理-SVN**:使用版本控制系统管理测试用例。 - **属性操作**:设置和管理测试对象的属性。 - **数据库操作**:连接数据库,执行SQL语句等。 - **数据文件操作**:读写数据文件。 - **循环入参**:使用...
SoapUI是一款广泛使用的开源接口测试工具,特别适合于Web Service接口的测试,包括SOAP和RESTful接口。在本文中,我们将深入探讨SoapUI的功能、如何使用它进行接口测试以及与cxf框架的关联。 首先,让我们了解什么...
它支持SOAP(Simple Object Access Protocol)和REST(Representational State Transfer)服务,能够帮助开发者和测试人员对WSDL(Web Services Description Language)格式的Web服务接口进行全面的功能测试和性能...
使用@SpringBootTest来搭建测试环境,使用@AutoConfigueMockMVC类实现Mock http操作 热部署和热加载 热部署代表的是:我们不需要重启服务器,能够将新war包替换旧的war包。 热加载代表的是:我们不需要...