Hi,
As you can see ( https://bugzilla.novell.com/show_bug.cgi?id=558697 ) there
missing test suite at all for some webclient plugins and status of the rest
plugins is not also in good shape. So I write here simple howto how you can
start with simple test.
1) use yast_model. (helps you to move functionality from controller to model,
so it is easier to test):
steps:
a) create simple model: (e.g. interface is
'org.opensuse.yast.modules.yapi.administrator')
class Basesystem < ActiveResource::Base
extend YastModel::Base
model_interface :'org.opensuse.yast.modules.yapi.administrator'
end
b)change lines which loads dynamic proxy to model:
load_proxy 'org.opensuse.yast.modules.yapi.administrator' # proxy_load
to
Model.find :one
# or :all depends if you have multiresource or single resource as
backend
2) create fixtures:
a) create xml which you want test as response from rest-service
b) save it in test/fixtures/*.xml
3) create simple unit test for model
require File.dirname(__FILE__) + '/../test_helper'
require 'yast_mock' #1)
class ModelTest < ActiveSupport::TestCase
def setup
response_bs = load_xml_response "basesystem.xml" #2
request_bs = load_xml_response "basesystem-response.xml" #3
ActiveResource::HttpMock.set_authentication
ActiveResource::HttpMock.respond_to do |mock|
header = ActiveResource::HttpMock.authentication_header
mock.resources :"org.opensuse.yast.modules.basesystem" => "/basesystem"
mock.permissions "org.opensuse.yast.modules.basesystem", {}
mock.get "/basesystem.xml", header, response_bs, 200
mock.post "/basesystem.xml", header, request_bs, 200
end
end
def test_find
test = Model.find :one
assert test.ready #4
assert_equal "test", test.response
end
def test_save
test = Model.find :one
test.value = "new"
test.save #5
end
def test_corner_case #6
response_bs = load_xml_response "basesystem_corner.xml"
ActiveResource::HttpMock.respond_to do |mock|
header = ActiveResource::HttpMock.authentication_header
mock.resources :"org.opensuse.yast.modules.basesystem" => "/basesystem"
mock.permissions "org.opensuse.yast.modules.basesystem", {}
mock.get "/basesystem.xml", header, response_bs, 200
end
test = Model.find :one
assert_equal "strange chars #...@$$%^&**", test.response
end
end
notes
1) needed for extension to HttpMock
2) it is xml which you get from rest service when you call show
3) it is xml which you get as response from update
4) just test attributes from ActiveResource. model is complete full featured
ActiveResource so it is possible to use all from that model
5) just call save is not enough, try find why ;)
6) test for corner case where you need to load different response from
rest-service. try find why this mock doesn't have post method mocked
Good and now you have your first initial version of test, which should grow
quite lot
If you have any difficulty or question, contact me ASAP. I can help you or
describe what you need.
Josef
--
Josef Reidinger
YaST team
maintainer of perl-Bootloader, YaST2-Repair, webyast
(language,time,basesystem,ntp)
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]