[Visual Studio]把偵錯下的數值,截取出來快速做成假資料,使用Object Exporter

[Visual Studio]把偵錯下的數值,截取出來快速做成假資料,使用Object Exporter

前言

不知道大家有沒有一種經驗,就是我們資料庫已經有即有資料,或者是串接別人演算法最後產生的一堆數據,當我們下偵錯的時候,很想把偵錯下看到的龐大資料取出來,有時候可能是取資料速度過慢,甚至是別人演算法寫得糟糕運算過程太久(噓),導致我們每次為了做後續的處理,都需要花上一點時間,甚至有時候我們是想把即有的假資料取出來,然後直接stub這個資料,來做單元測試,這篇就來介紹一個工具,可以快速的把假資料產出來。

安裝方式

我們可以在visual studio的Extensions and Updates直接安裝,搜尋Object Exporter

安裝完之後就來模擬一下,我現在在偵錯模式看到一個物件,然後想把此物件的資料直接取出來,並寫成C#的程式碼,那應該怎麼做呢,請見圖示例

這是偵錯下看到的十筆資料

可以直接用選取的

或者是在第二個頁籤自行打上要截取的變數

選擇你想要匯出的格式,比如是要C#或者是JSON甚至是XML的

C#格式

var sports = sports = new System.Collections.Generic.List<BookStore.Service.SportDto>
{
	new BookStore.Service.SportDto
	{
		Name = "9Balls",
		OrderNo = 1,
		Sbc = "Others",
		ShortName = "9B"
	},
	new BookStore.Service.SportDto
	{
		Name = "American Football",
		OrderNo = 2,
		Sbc = "Basketball/NFL",
		ShortName = "AFB"
	},
	new BookStore.Service.SportDto
	{
		Name = "BaseBall",
		OrderNo = 3,
		Sbc = "Baseball",
		ShortName = "B.Ball"
	},
	new BookStore.Service.SportDto
	{
		Name = "Beach Soccer",
		OrderNo = 4,
		Sbc = "Soccer",
		ShortName = "B.SOC"
	},
	new BookStore.Service.SportDto
	{
		Name = "Beach Volleyball",
		OrderNo = 5,
		Sbc = "Volleyball",
		ShortName = "B.VOL"
	},
	new BookStore.Service.SportDto
	{
		Name = "Badminton",
		OrderNo = 6,
		Sbc = "Badminton",
		ShortName = "BADM"
	},
	new BookStore.Service.SportDto
	{
		Name = "Basketball",
		OrderNo = 7,
		Sbc = "Basketball/NFL",
		ShortName = "BB"
	},
	new BookStore.Service.SportDto
	{
		Name = "Boxing",
		OrderNo = 8,
		Sbc = "Others",
		ShortName = "BOX"
	},
	new BookStore.Service.SportDto
	{
		Name = "Cricket",
		OrderNo = 9,
		Sbc = "Others",
		ShortName = "CRIC"
	},
	new BookStore.Service.SportDto
	{
		Name = "Cycling",
		OrderNo = 10,
		Sbc = "Others",
		ShortName = "CYC"
	}
};

JSON格式

"sports": 
[
	{
		"Name": "9Balls",
		"OrderNo": 1,
		"Sbc": "Others",
		"ShortName": "9B"
	}, 
	{
		"Name": "American Football",
		"OrderNo": 2,
		"Sbc": "Basketball/NFL",
		"ShortName": "AFB"
	}, 
	{
		"Name": "BaseBall",
		"OrderNo": 3,
		"Sbc": "Baseball",
		"ShortName": "B.Ball"
	}, 
	{
		"Name": "Beach Soccer",
		"OrderNo": 4,
		"Sbc": "Soccer",
		"ShortName": "B.SOC"
	}, 
	{
		"Name": "Beach Volleyball",
		"OrderNo": 5,
		"Sbc": "Volleyball",
		"ShortName": "B.VOL"
	}, 
	{
		"Name": "Badminton",
		"OrderNo": 6,
		"Sbc": "Badminton",
		"ShortName": "BADM"
	}, 
	{
		"Name": "Basketball",
		"OrderNo": 7,
		"Sbc": "Basketball/NFL",
		"ShortName": "BB"
	}, 
	{
		"Name": "Boxing",
		"OrderNo": 8,
		"Sbc": "Others",
		"ShortName": "BOX"
	}, 
	{
		"Name": "Cricket",
		"OrderNo": 9,
		"Sbc": "Others",
		"ShortName": "CRIC"
	}, 
	{
		"Name": "Cycling",
		"OrderNo": 10,
		"Sbc": "Others",
		"ShortName": "CYC"
	} 
] 

XML格式

<?xml version="1.0" encoding="utf-8"?>
<sports>
	<BookStore.Service.SportDto>
		<Name>"9Balls"</Name>
		<OrderNo>1</OrderNo>
		<Sbc>"Others"</Sbc>
		<ShortName>"9B"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"American Football"</Name>
		<OrderNo>2</OrderNo>
		<Sbc>"Basketball/NFL"</Sbc>
		<ShortName>"AFB"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"BaseBall"</Name>
		<OrderNo>3</OrderNo>
		<Sbc>"Baseball"</Sbc>
		<ShortName>"B.Ball"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Beach Soccer"</Name>
		<OrderNo>4</OrderNo>
		<Sbc>"Soccer"</Sbc>
		<ShortName>"B.SOC"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Beach Volleyball"</Name>
		<OrderNo>5</OrderNo>
		<Sbc>"Volleyball"</Sbc>
		<ShortName>"B.VOL"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Badminton"</Name>
		<OrderNo>6</OrderNo>
		<Sbc>"Badminton"</Sbc>
		<ShortName>"BADM"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Basketball"</Name>
		<OrderNo>7</OrderNo>
		<Sbc>"Basketball/NFL"</Sbc>
		<ShortName>"BB"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Boxing"</Name>
		<OrderNo>8</OrderNo>
		<Sbc>"Others"</Sbc>
		<ShortName>"BOX"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Cricket"</Name>
		<OrderNo>9</OrderNo>
		<Sbc>"Others"</Sbc>
		<ShortName>"CRIC"</ShortName>
	</BookStore.Service.SportDto>
	<BookStore.Service.SportDto>
		<Name>"Cycling"</Name>
		<OrderNo>10</OrderNo>
		<Sbc>"Others"</Sbc>
		<ShortName>"CYC"</ShortName>
	</BookStore.Service.SportDto>
</sports>

結論

實際上取決於你物件的大小還有資料的複雜度,有可能需要很久的時間,有了這個工具,我們如果想要針對舊專案做單元測試,也方便了許多,如果各位有什麼更好工具的話,再請給我一點建議。