Module: MockHelper

Includes:
ISandBoxHelper, JsonHelper
Defined in:
lib/common/mock_helper.rb

Overview

封装mtop结果mock的核心模块

Instance Method Summary collapse

Methods included from ISandBoxHelper

#ipull_file, #ipush_data, #ipush_file, #iremove_file

Methods included from JsonHelper

#parse_json, #to_json

Instance Method Details

#istart_mock(what, fn = nil) ⇒ Object

功能:

iPhone mock数据关键字,可多次调用,不会冲掉上次的mock

参数解释:

what 此处填string内容/此处填PC上数据文件的路径/Hash

Example:

Example #1:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/common/mock_helper.rb', line 107

def istart_mock(what, fn=nil)
    @mobile_mock_folder = '/tmp/mtopmock'
    if what.class == String
        begin
            json_data = JSON.parse what.dup
            data = what.to_s
            fail 'api is missing' if json_data['api'].nil?
            if fn
                file_name = fn
            else
                file_name = json_data['api']
            end

            ipush_data @mobile_mock_folder, data, file_name
        rescue
            begin
                if File.file? what.to_s
                    data = IO.read(what.to_s)
                    json_data = JSON.parse(data)
                    fail 'api is missing' if json_data['api'].nil?
                    if fn
                        file_name = fn
                    else
                        file_name = json_data['api']
                    end

                    ipush_data @mobile_mock_folder, data, file_name
                else
                    fail 'content is illegal'
                end
            rescue
                raise Exception, "error:#{$!} at:#{$@}"
            end

        end
    elsif what.is_a? Hash
        fail 'api is missing' if what['api'].nil?
        data = what.to_json.to_s
        if fn
            file_name = fn
        else
            file_name = what['api']
        end

        ipush_data @mobile_mock_folder, data, file_name
    end
end

#istop_mockObject

功能:

iPhone 删除mock文件,停止mock数据关键字,只需调用一次,建议在teardown中调用

参数解释:

Example:

Example #1:



163
164
165
166
# File 'lib/common/mock_helper.rb', line 163

def istop_mock
    @mobile_mock_folder = '/tmp/mtopmock'
    iremove_file @mobile_mock_folder
end

#start_mock(what, fn = nil) ⇒ Object

功能:

用于alitrip客户端mock数据,推送mock数据到手机,可多次调用,不会冲掉之前的mock

参数解释:

what 此处填string内容/此处填PC上数据文件的路径/Hash

Example:

Example #1: mock ‘string content’ Example #2: mock /filepath/on/PC



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/common/mock_helper.rb', line 25

def start_mock(what, fn=nil)

    system 'adb shell dumpsys activity top trip start mtop_mock_response > /dev/null 2>&1'
    mobile_mock_folder = '/mnt/sdcard/AlitripMockData/'

    if what.class == String
        begin
            json_data = JSON.parse what.dup
            fail 'api is missing' if (json_data['api'].nil? and fn.nil?)
            if fn
                file_name = fn
            else
                file_name = json_data['api'] + '.json'
            end

            data = what.to_s
        rescue
            begin
                data = IO.read(what.to_s)
                json_data = JSON.parse data
                fail 'api is missing' if (json_data['api'].nil? and fn.nil?)
                if fn
                    file_name = fn
                else
                    file_name = json_data['api'] + '.json'
                end

            rescue
                raise Exception, 'file not exist'
            end
        end
    elsif what.is_a? Hash
        fail 'api is missing' if (what['api'].nil? and fn.nil?)

        data = what.to_json.to_s

        if fn
            file_name = fn
        else
            file_name = data['api'] + '.json'
        end

    end

    mock_path = mobile_mock_folder + file_name
    begin
        tmp_file = '.mockFile'
        File.open(tmp_file, "w+") do |file|
            file.puts data
        end
        system "adb push #{tmp_file} #{mock_path} > /dev/null 2>&1"
    rescue
        raise Exception, "error:#{$!} at:#{$@}"
    ensure
        system "rm -rf #{tmp_file} > /dev/null 2>&1"
    end
    #
    # push_file mock_path, data

end

#stop_mockObject

功能:

删除mock数据,只需要调用一次,建议在teardown中调用

参数解释:

Example:

Example #1:



94
95
96
97
# File 'lib/common/mock_helper.rb', line 94

def stop_mock()
    system 'adb shell dumpsys activity top trip stop mtop_mock_response > /dev/null 2>&1'
    system 'adb shell rm -rf /sdcard/AlitripMockData'
end