ฉันยังใหม่กับ Moq และฉันต้องการเขียนการทดสอบหน่วยโดยใช้มัน ฉันมีฐานข้อมูลที่มีตารางไม่กี่ตาราง เช่น:
EducationUser | Application
- UsrName - Student
- UsrPwd - CourseId
- UsrChallenge - Date
- IsTeacher - Grade
- FullName
นี่คือฐานข้อมูลบน localdb
ซึ่งฉันต้องการจำลอง ฉันได้สร้างเอนทิตีโดยใช้ Entity Framework อินเทอร์เฟซของเอนทิตีเหล่านี้คือ IEducationEntities
ตอนนี้ฉันต้องการสร้างวัตถุจำลองและทดสอบบริการทางเว็บบางอย่าง เช่น:
[TestMethod()]
public void LoginTest()
{
HttpResponseMessage response = Request.CreateResponse(_accountController.Login("andrew", "DefaultPassword"));
Assert.IsTrue(response.IsSuccessStatusCode, "User unable to log in with correct login info");
}
สำหรับสิ่งนี้ จากสิ่งที่ฉันเข้าใจจากเอกสารประกอบ ฉันควรจะ สามารถทำอะไรบางอย่างเช่น:
[TestClass()]
public class AccountControllerTests : ApiController
{
Mock<IEducationEntities> _entities = new Mock<IEducationEntities>(MockBehavior.Strict);
private AccountController _accountController;
public AccountControllerTests() {
_accountController = new AccountController(_entities.Object);
_entities.Setup(table => table.EducationUsers.UsrName).Returns("andrew");
_entities.Setup(table => table.EducationUsers.UsrPwd).Returns("DefaultPassword");
}
[TestMethod] //etc, defining tests below
อย่างไรก็ตาม สิ่งนี้ไม่ได้ผลเลย เนื่องจากเอนทิตีที่สร้างจาก databse ไม่มีข้อมูลเกี่ยวกับฟิลด์ย่อยอย่างชัดเจน และฉันได้รับข้อผิดพลาด:
'DbSet' ไม่มีคำจำกัดความสำหรับ 'UsrPwd' และไม่มีวิธีการขยาย 'UsrPwd' ที่ยอมรับอาร์กิวเมนต์แรกของประเภท 'DbSet' (คุณขาดคำสั่งการใช้หรือการอ้างอิงแอสเซมบลีหรือไม่)
ฉันพลาดอะไรไป? จะเติมวัตถุ moq
ด้วยข้อมูลทดสอบที่มีโครงสร้างเดียวกันกับฐานข้อมูลของฉันได้อย่างไร
UsrPassword
แต่คุณเรียกว่าUsrPwd
ในโค้ดทดสอบ/จำลองของคุณ - person Ben Rubin   schedule 20.05.2017