前天写了文章:
有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
分享到:
相关推荐
SAP webservice 通过soapui的测试方法 SAP webservice 通过soapui的测试方法
WebService Mock是用于模拟WebServices的非常简单但功能强大的工具。 为什么要使用WebService模拟? 通过非侵入性方式实现模拟,甚至无需编写任何代码行,这意味着无需模拟服务存根。 可以用于对应用程序进行压力...
webservice测试调用工具WebserviceStudio20
运行main程序即可测试webservice ,包括3个例子,解压缩即可。
Loadrunner Webservice压力测试步骤示例,有截图
Postman 在线测试webservice服务
webservice的一个测试例子,为了方便以后使用,在此存储一下
WebService编写、调用测试工具WebService编写、调用测试工具
SOAPUI测试WEBSERVICE使用手册
WebServiceStudio WebService测试工具
webservice安全测试整理
利用C#实现webservice测试工具
webservice简单测试工具接口测试 webservice 测试 客户端
使用SOAPUI对WEBSERVICE进行性能测试
webservice wcf 等接口调试工具。webservice wcf 等接口调试工具。
好用的webservice测试工具,直接输入想要测试的webservice地址,即可列出所有的方法,输入想要测试的参数,点击调用即可进行测试。
测试webService;webservice测试调用工具WebserviceStudio20
内置使用教程,下载进行WebService的测试使用
可以使用这个工具来测试WebService接口,使用起来很方便也很简单
webservice测试调用工具WebserviceStudio20