English 中文(简体)
一体化测试/单位测试——如何决定
原标题:Integration testing / Unit testing - How to decide

I have controller API methods for which I have to write test case methods. As of now, I ve been writing integration test methods for the project. I m not sure how to frame unit test case methods for API methods like below:

www.un.org/chinese/sc/presidency.asp

[HttpPost]
public IHttpActionResult PostCancelEmployee(EmployeeRequest req)
{
    string id = req.EmployeeId;
    EmployeeDetails mgr = new EmployeeDetails(id);
    response = mgr.CancelEmployeeDetails(req);
}

如果我撰写一体化测试案例,那么抽样投入/要求参数就会根据数据库变化,导致测试案例失败。 谁能帮助我用Moq框架为这些类型的APIC方法撰写单位测试案例?

问题回答

微软文件在使用Moq对APIC控制器方法进行单位测试方面有一些很好的例子。 下面是你所领导的控制者可能认为什么的基本例子。

www.un.org/Depts/DGACM/index_spanish.htm

public class EmployeeController : ApiController
{
    IEmployeeRepository _repository;

    public EmployeeController(IEmployeeRepository repository)
    {
        _repository = repository;
    }

    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        Employee employee = _repository.GetById(id);
        if (employee == null)
        {
            return NotFound();
        }
        return Ok(employee);
    }

    [HttpPost]
    public IHttpActionResult Post(Employee employee)
    {
        _repository.Add(employee);
        return CreatedAtRoute("DefaultApi", new { id = employee.Id }, employee);
    }
    
    [HttpDelete]
    public IHttpActionResult Delete(int id)
    {
        _repository.Delete(id);
        return Ok();
    }

    [HttpPut]
    public IHttpActionResult Put(Employee employee)
    {
        // Do some work (not shown).
        return Content(HttpStatusCode.Accepted, employee);
    }
}

这里最大的变化是,将“你”类别中的usage与数据库互动,从 娱乐/>。 您的榜样是,在您的APIC方法中,你正在即时推出一个新的<代码>EmployeeDetails。 最好把任何附属物作为接口注入你的控制器,以便能够在你的测试中轻易加以改动。

此处为核查<代码>的单位试验 雇员/编码 方法回归类型和数据:

[TestMethod]
public void GetReturnsEmployeeWithSameId()
{
    // Arrange
    var mockRepository = new Mock<IEmployeeRepository>();
    mockRepository.Setup(x => x.GetById(42))
        .Returns(new Employee { Id = 42 });

    var controller = new EmployeeController(mockRepository.Object);

    // Act
    IHttpActionResult actionResult = controller.Get(42);
    var contentResult = actionResult as OkNegotiatedContentResult<Employee>;

    // Assert
    Assert.IsNotNull(contentResult);
    Assert.IsNotNull(contentResult.Content);
    Assert.AreEqual(42, contentResult.Content.Id);
}

其他测试例子,包括其余的APIC方法,均列入相关文件。

作为what的测试,一般建议验证:

  • The action returns the correct type of response.
  • Invalid parameters return the correct error response.
  • The action calls the correct method on the repository or service layer.
  • If the response includes a domain model, verify the model type.




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签