Skip to content

Instantly share code, notes, and snippets.

@jsuo
Last active December 29, 2015 02:18
Show Gist options
  • Save jsuo/7599135 to your computer and use it in GitHub Desktop.
Save jsuo/7599135 to your computer and use it in GitHub Desktop.
社員モデルクラスのテストコード
#coding: windows-31j
require './exlap'
class Employee
attr_accessor :id, :last_name, :first_name
def full_name
return @last_name + " " + @first_name
end
end
#ADODB.Recordsetを拡張
module Recordset
def [] field
self.Fields.Item(field).Value
end
def []= field,value
self.Fields.Item(field).Value = value
end
def each_record
if self.EOF or self.BOF
return
end
self.MoveFirst
until self.EOF or self.BOF
yield self
self.MoveNext
end
end
end
describe "mdlEmployeeの単体テスト" do
before(:all) do
xlApp = Exlap.new
book_name = "受注検索.xlsm"
@wb = xlApp.book_open book_name
test_module = <<EOF
Dim employee As New mdlEmployee
Dim resultSet() As mdlEmployee
Dim recordsCount As Long
Function testFullName(ByVal index As Integer) As String
resultSet = employee.findAll
testFullName = resultSet(index).fullName
End Function
Function testEmployeeId(ByVal index As Integer) As String
resultSet = employee.findAll
testEmployeeId = resultSet(index).id
End Function
Function testEmployeeCount() As Long
employee.findAll recordsCount
testEmployeeCount = recordsCount
End Function
EOF
#TestModuleという名前で標準モジュールを追加する
@wb.macro_add(test_module, 1, "TestModule")
end
after(:all) do
@wb.close
end
before(:each) do
accdb_path = File.dirname(__FILE__) + "/" + "ノースウィンド.accdb"
connection_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
@cn = WIN32OLE.new("ADODB.Connection")
@cn.Open connection_string + accdb_path
@cn.BeginTrans
sql = "SELECT ID, 姓, 名 FROM 社員"
rs = @cn.Execute(sql)
rs.extend Recordset
@employees = Array.new
rs.each_record do |rs|
employee = Employee.new
employee.id = rs["ID"]
employee.last_name = rs["姓"]
employee.first_name = rs["名"]
@employees << employee
end
rs.close
@lastIndex = @employees.size.to_i - 1
end
after(:each) do
@cn.RollbackTrans
@cn.close
end
describe "社員情報 1件目を取得 :" do
first = 0
it "フルネームが合致すること" do
result = @wb.run("TestModule.testFullName", first)
result.should == @employees[first].full_name
end
it "社員IDが合致すること" do
result = @wb.run("TestModule.testEmployeeId", first)
result.should == @employees[first].id.to_s
end
end
describe "社員情報 2件目を取得 :" do
second = 1
it "フルネームが合致すること" do
result = @wb.run("TestModule.testFullName", second)
result.should == @employees[second].full_name
end
it "社員IDが合致すること" do
result = @wb.run("TestModule.testEmployeeId", second)
result.should == @employees[second].id.to_s
end
end
describe "社員情報 最終件目を取得 :" do
it "フルネームが合致すること" do
result = @wb.run("TestModule.testFullName", @lastIndex)
result.should == @employees[@lastIndex].full_name
end
it "社員IDが合致すること" do
result = @wb.run("TestModule.testEmployeeId", @lastIndex)
result.should == @employees[@lastIndex].id.to_s
end
end
describe "社員情報 件数取得 :" do
it "件数が合致すること" do
result = @wb.run("TestModule.testEmployeeCount")
result.should == @employees.size
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment