我已经为我的脚本实现了一个测试用例,当我在Webstorm中使用mocha的配置执行它时它运行得非常好.
我的测试脚本的名称是adminTest.js.
现在我喜欢使用npm脚本从控制台或稍后从构建服务器运行它.
因此我在package.json中创建了一个脚本条目
这是完整的文件:
{
"name": "cdh",
"version": "0.0.1",
"description": "CDH connector",
"main": "cdh.js",
"private" : true,
"dependencies": {
"cli-color": "^1.0.0",
"handlebars": "^4.0.3",
"sync-request": "^2.0.1",
"jslint": "^0.9.3",
"xmldom": "^0.1.19",
"xpath": "^0.0.9",
"mocha": "2.3.3 ",
"proxyquire": "1.7.3",
"mocha-sinon": "1.1.4",
"jasmine" : "2.3.2",
"chai" : "3.4.1"
},
"devDependencies": {},
"scripts": {
"test": "node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec"
},
"author": "kme",
"license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)
当我npm run test
从控制台启动脚本时,会导致此错误:
> cdh@0.0.1 test C:\src\trunk\scripts\testing
> node ./node_modules/mocha/bin/mocha tests/**/*Test.js --reporter spec
npm ERR! Windows_NT …
Run Code Online (Sandbox Code Playgroud) 我想从调用方法中获取一个 MethodInfo 对象,以确定该方法是否设置了特殊属性。
具有调用方法 Run() 的 Programm 类
class Program
{
private static RestHandler _handler = new RestHandler();
static void Main(string[] args)
{
Run();
}
[Rest("GET")]
static void Run()
{
_handler.Handler(typeof(Program));
}
}
Run Code Online (Sandbox Code Playgroud)
我想确定自定义属性的类
public class RestHandler
{
public void Handler(Type t)
{
StackFrame frame = new StackFrame(1);
var method = frame.GetMethod();
MethodInfo methodInfo = t.GetMethod(method.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
var attributes = methodInfo.GetCustomAttributes<RestAttribute>();
}
}
Run Code Online (Sandbox Code Playgroud)
属性类
public class RestAttribute : Attribute
{
public RestAttribute(string method)
{
Method = …
Run Code Online (Sandbox Code Playgroud) 由于我的sut类具有带有IUnityContainer
as参数的构造函数,因此我想在单元测试中设置一个新的Unity容器并将其传递给sut。我喜欢在这里使用Moq,因为对于某些方法调用,我也必须非常认真。我在这里尝试过的是
public interface IFoo
{
void Bar();
}
class Program
{
static void Main(string[] args)
{
var fooMock = new Mock<IFoo>();
fooMock.Setup(x => x.Bar()).Callback(() => Console.WriteLine("Hello"));
IUnityContainer container = new UnityContainer();
container.RegisterType(typeof (IFoo), fooMock.Object.GetType());
var sut = container.Resolve<IFoo>();
sut.Bar();
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
但是这导致的ResulutionFailedException
。有什么想法我可以做什么,或者可以替代这个问题?
我喜欢使用karma-coverage为我的打字稿源文件生成一份报道.我的单元测试是用javascript编写的,我使用的是Jasmine Test框架.
我的文件夹结构如下所示:
node_modules/
app/
app.js
app.js.map
app.ts
components/
controllers/
SampleController.ts
directives/
filters/
services/
unittests/
karma.conf.js
components/
controllers/
SampleControllerTest.js
directives/
filters/
services/
Run Code Online (Sandbox Code Playgroud)
我的karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
plugins: [
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-coverage',
'karma-phantomjs-launcher',
'karma-sourcemap-loader'
],
preprocessors: {
'../app/directives/controls/**/*Template.html' : [ 'ng-html2js' ],
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'../app/app.js' : ['sourcemap', 'coverage' ],
},
reporters: ['progress', 'coverage'],
// web …
Run Code Online (Sandbox Code Playgroud) javascript typescript karma-runner karma-jasmine karma-coverage
c# ×2
javascript ×2
karma-runner ×1
methodinfo ×1
mocha.js ×1
moq ×1
node.js ×1
npm ×1
reflection ×1
stack-frame ×1
testing ×1
typescript ×1
unit-testing ×1