๋๋ (ab) Autofixture๋ฅผ ์ฌ์ฉํ์ฌ Web API์ ๋ํ ๋ฌธ์ ์์ ๋ฅผ ์์ฑํฉ๋๋ค. Autofixture๊ฐ ์์ฑํ๋ ๋ฉ์์ง(๋ช ๋ น ๋ฐ ์ฟผ๋ฆฌ)๋ JSON์ผ๋ก ์ง๋ ฌํ๋๊ณ ๋ฌธ์์ ์ผ๋ถ๊ฐ ๋ฉ๋๋ค. Web API์ฉ VS ํ ํ๋ฆฟ์ด /HelpPage ์์ญ์์ ์์ฑํ๋ ์๋ฐฑ ์ค์ ์ถ์ ํ ์ฝ๋์ ๋นํด ์ด๊ฒ์ ์ ๋ง ์ ์๋ํ๋ฉฐ ๋ช ์ค์ ์ฝ๋๊ฐ ํ์ํฉ๋๋ค.
ํ์ฌ ์ด๋ฅผ ๋ฌ์ฑํ๊ธฐ ์ํด ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
``` C#
๊ฐ์ธ ์ ์ ์ฝ๊ธฐ ์ ์ฉ MethodInfo CreateMethodInfo =
GetMethod(() => SpecimenFactory.Create
.GetGenericMethodDefinition();
๊ณต์ฉ ์ ์ ๊ฐ์ฒด ์์ฑ(์ ํ ์ ํ)
{
var ๊ณ ์ ์ฅ์น = new Fixture();
์ ์ ์ธ๋ฑ์ค = 1;
Fixture.Register(() => "์ํ ํ
์คํธ" + index++);
return CreateMethodInfo.MakeGenericMethod(type).Invoke(null, new object[] { fixture });
}
private static MethodInfo GetMethod(์
((MethodCallExpression)methodCall.Body).Method;
```
์ปดํ์ผ ์ ์ ํํ ๋ฉ์์ง๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ Autofixture์ Fixture.Create<T>()
๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์์ ์ฝ๋์์ ์ ์ ์๋ฏ์ด ํ์ฌ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ ๋ฆฌํ๋ ์
์ ์ฌ์ฉํ์ฌ ์ด ์ผ๋ฐ Create<T>
๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ์
๋๋ค.
๋ด ๋ชฉํ๋ฅผ ๋ฌ์ฑํ๋ ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ด ์์ต๋๊น? ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ์ผ๋ฐ์ด ์๋ Create(Type)
๋ฉ์๋๋ฅผ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ถ๊ฐํ ์ ์์ต๋๊น?
์ด ์๋ฃจ์ ์ด ํจ๊ณผ๊ฐ ์์๊น์?
c#
var specimen = new SpecimenContext(fixture).Resolve(type);
๋ฉ์๋! ๊ทธ๊ฒ์ ํธ๋ฆญ์ ์ํํฉ๋๋ค! ๊ฐ์ฌ ํด์.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ์๋ฃจ์ ์ด ํจ๊ณผ๊ฐ ์์๊น์?
c# var specimen = new SpecimenContext(fixture).Resolve(type);