我对JS很新,但我正在努力学习.事实上,我试图模仿表达.这是我的基类(为了测试目的而减少):
import compression from 'compression';
import express from 'express';
export default class Index{
constructor(){}
spawnServer(){
console.log(express());
let app = express();
app.use(STATIC_PATH, express.static('dist'));
app.use(STATIC_PATH, express.static('public'));
etc...
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图在一个单独的测试文件中实现的测试...:
test('should invoke express once', () =>{
index.spawnServer();
expect(mockExpressFuncs().use.mock.calls.length).toBe(3);
})
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 如何使测试覆盖被测试类的要求 - 甚至可能吗?我希望我的索引使用express的模拟版本,包括express()和express.require.
我确实阅读了文档,尝试过类似的东西:
const mockFunction = function() {
return {
use: useFn,
listen: jest.fn()
};
};
beforeEach(() => {
jest.mock('express', () => {
return mockFunction;
})
express = require('express');
});
Run Code Online (Sandbox Code Playgroud)
但这不起作用 - 我做错了什么?:(
谢谢.
我知道这很简单,我可能遗漏了一些东西......假设我想为生成的控件提供ID,当我的模型是整数列表时:
@model List<Int>
...
foreach(int number in Model)
{
<div id="box + @number"></div>
}
...
Run Code Online (Sandbox Code Playgroud)
box + @number实际上给了我:id ="box + 1","box + 2"等.当我想要:"box1","box2".我究竟做错了什么?
在我的测试中,这是我的代码:
[SetUp]
public void Initialise()
{
mockOwinManager = new Mock<IOwinManager<ApplicationUser, ApplicationRole>>();
mockSearch = new Mock<ISearch<ApplicationUser>>();
mockMail = new Mock<IRpdbMail>();
mockUserStore = new Mock<IUserStore<ApplicationUser>>();
mockOwinManager.Setup(x => x.UserManager).Returns(() => new AppUserManager(mockUserStore.Object));
sut = new UsersController(mockOwinManager.Object, mockSearch.Object, mockMail.Object);
}
Run Code Online (Sandbox Code Playgroud)
然后测试本身:
[Test]
public void WhenPut_IfUserIsNullReturnInternalServerError()
{
//Arrange
mockOwinManager.Setup(x => x.UserManager.FindByIdAsync(It.IsAny<string>())).Returns(() => null);
//Act
var response = sut.Put(new AppPersonUpdate());
//Assert
Assert.AreEqual(response.Result.StatusCode, HttpStatusCode.InternalServerError);
}
Run Code Online (Sandbox Code Playgroud)
但是我的安排行抛出以下错误:
无法实例化以下类的代理:Microsoft.AspNet.Identity.UserManager`1 [[SWDB.BusinessLayer.Identity.ApplicationUser,SWDB.BusinessLayer,版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null]]。找不到无参数的构造函数。
为什么会这样,因为在安装程序中,我已经将我的嘲讽OwinManager的UserManager属性设置为我希望它返回的值?
试图在我的代码中运行此查询:
var rslt = ent.AppointmentDiaries.Where(s => s.DateTimeScheduled >=
fromDate && EntityFunctions.AddMinutes(
s.DateTimeScheduled, s.AppointmentLength) <= toDate);
Run Code Online (Sandbox Code Playgroud)
它不断分手:
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime] AddMinutes(System.Nullable`1[System.DateTime], System.Nullable`1[System.Int32])' method, and this method cannot be translated into a store expression.
Run Code Online (Sandbox Code Playgroud)
使用entityframework 6.1.0 ...
我知道规范的连接,所以我希望EntityFunctions.AddMinutes能够使用codefirst查询......
知道我做错了什么吗?
考虑以下测试:
[TestCase(2016, true)]
[TestCase(2017, false)]
[TestCase(2018, false)]
[TestCase(2019, false)]
[TestCase(2020, true)]
public void When_IsLeapYear_ReturnTrueForLeapYear(int year, bool expectedResult)
{
//Act
var result = _sut.IsLeapYear(year);
//Assert
Assert.AreEqual(result, expectedResult);
}
Run Code Online (Sandbox Code Playgroud)
在这样的测试用例中包含年份和预期结果是不是一种不好的做法,而不是创建两个不同的测试(例如,一个用于期望真,一个用于期望错误?)
谢谢