What is the simplest way to run a timer-triggered Azure Function locally once?

后端 未结 7 885
醉话见心
醉话见心 2020-12-13 12:03

I have a few C# Azure Functions that run on a schedule using timer triggers. I\'ve set them up like so, where %TimerSchedule% refers to a cron expression in the

7条回答
  •  情歌与酒
    2020-12-13 12:28

    I had the same question. I fixed it with a Unittest. Indeed you need to stub out the TraceWriter and the TimerInfo.

    Here some code how I did this.

    TimerInfo:

    public class ScheduleStub : TimerInfo
    {
        public ScheduleStub(TimerSchedule schedule, ScheduleStatus status, bool isPastDue = false) : base(schedule, status, isPastDue)
        {
        }
    }
    

    And the TraceWriter:

     public class TraceWriterStub : TraceWriter
    {
        protected TraceLevel _level;
        protected List _traces;
    
        public TraceWriterStub(TraceLevel level) : base(level)
        {
            _level = level;
            _traces = new List();
        }
    
        public override void Trace(TraceEvent traceEvent)
        {
            _traces.Add(traceEvent);
        }
    
        public List Traces => _traces;
    }
    

提交回复
热议问题