我正在尝试在测试中实现 fetch 模拟函数。按照教程,我尝试使用以下方法来做到这一点jest.fn():
const fetchFunction = jest.fn(() => {
return null
})
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这不起作用。如果我只是console.log(fetchFunction)在测试中这样做,我会得到:
[Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function]
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试通过console.log(fetchFunction())我得到的来调用它undefined?
我正在尝试在 create-react-app 文件夹中执行此操作。我需要安装额外的东西吗?关于为什么会发生这种情况有什么想法吗?
在haskell中,使用section来部分应用中缀函数是可行的,例如,给定中缀函数<(小于)可以部分应用任何函数的参数:(5 <),(<5)
换句话说,在haskell中我们有以下简写符号:
op :: a -> b -> c
(`op` y) === \x -> x `op` y
(x `op`) === \y -> x `op` y
Run Code Online (Sandbox Code Playgroud)
F#有类似的概念吗?
鉴于以下代码,
public T Execute<T>(Func<T> methodParam)
{
return methodParam ();
}
public void CallMethodsAnonymously<T>()
{
T result = Execute(() => _service.SomeMethod1());
T result1 = Execute(() => _service.SomeMethod2(someParm1));
T result2 = Execute(() => _service.SomeMethod3( someParm1, someParm2));
}
Run Code Online (Sandbox Code Playgroud)
从Execute方法,是否可以检查"methodParam"并提取或确定匿名函数体内的参数数量?例如,是否可以从Execute方法中确定someParam1和someParam2的值?
如果我有一个产品类:
public class Product
{
public string Title { get; set; }
public string Make { get; set; }
public Decimal Price { get; set; } //(Edit) - Added non-string
}
Run Code Online (Sandbox Code Playgroud)
我在另一个类中有一个属性声明为:
Func<Product, object> SortBy { get; set; }
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法设置SortBy:
SortBy = p => p.Title;
Run Code Online (Sandbox Code Playgroud)
但是,如果我将SortBy的属性名称存储为字符串,我将如何使用反射进行相同的赋值
string sortField = "Title";
SortBy = /*Some reflection using sortField*/;
Run Code Online (Sandbox Code Playgroud) 我正在重新分解一些使用以下构造的代码.我希望能够做到这一点,所以我可以在Func中有其他参数类型.
IDictionary<string, Func<Employee, string>>
Run Code Online (Sandbox Code Playgroud)
我试过用:
IDictionary<string, Func<Object, string>>
Run Code Online (Sandbox Code Playgroud)
然后把它扔回去:
(IDictionary<string, Func<Object, string>>)myDictionary
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外,说我无法施放
无法转换类型为System.Collections.Generic.Dictionary
2[System.String,System.Func2的对象...
我一直在玩仿制药,但这似乎只是让我觉得我无法在课堂上设置和通用字段
private IDictionary<string, Func<T, string>> lineSpecification;
Run Code Online (Sandbox Code Playgroud)
在圈子四处走动所以任何指针都会受到赞赏.
编辑:
这是我得到的完整异常消息:
System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Func`2[DataObjects.Employee,System.String]]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
Run Code Online (Sandbox Code Playgroud) 这应该是一个问前的问题,我搜索过但我找不到任何答案.对不起,如果它是重复的.我有一个查询可以说:
my_query=session.query(Item).filter(somefilter)
Run Code Online (Sandbox Code Playgroud)
现在,Item有一个列,让我们说counter,我想找到这一列的总和my_query.我可以这样做:
sum=0
for row in query:
sum+=row.counter
Run Code Online (Sandbox Code Playgroud)
但我不这样做这是在大型数据库中特别有效的方法.我知道这是可能的:sqlalchemy简单的例子是`sum`,`average`,`min`,`max`,但是这需要过滤qry(从页面借来的)我已经给出了过滤版本my_query.我不知道在qryvs使用for循环之上再次进行过滤是否真的更有效my_query.
我使用此方法获取方法中属性的值:
public static T Decrypt<T>(Func<T> prop, string username, string password)
{
T value = prop();
//do cool stuff with t
return value;
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来做另一种方式arround,设置我的属性的价值
public static void Encrypt<T>(Func<T> prop, T value, string username, string password)
{
//do stuff with value
??? set prop ???
}
Run Code Online (Sandbox Code Playgroud)
我搜索并尝试过表达式,但云无法让它工作:
public static void Encrypt<T>(Expression<Func<T>> property, T value, string username, string password)
{
//do stuff with value
var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;
propertyInfo.SetValue(property, value);
}
Run Code Online (Sandbox Code Playgroud) 我想检查是否已使用NSubstitute调用匿名函数.类I中的方法有一个Func<>参数,我想确保调用(或不调用)此参数.我尝试了以下,但它似乎不起作用:
var spy = Substitute.For<Func<string, int>>();
MyClass.DoSomething(spy);
spy.Invoke(Arg.Any<string>()).Received();
Run Code Online (Sandbox Code Playgroud)
然而,这引发了一个例外:
NSubstitute.Exceptions.NullSubstituteReferenceException : NSubstitute extension methods like .Received can only be called on objects created using Substitute.For<T>() and related methods.
Run Code Online (Sandbox Code Playgroud) 嗨我有一个简单的存储库层,其方法如下:
public Questionnaire GetQuestionnaire(Expression<Func<Question, bool>> predicateFunc)
{
return new Questionnaire
{
Title = "Geography Questions",
Questions = GetQuestionnaireData().Where(predicateFunc.Compile()).ToList()
};
}
Run Code Online (Sandbox Code Playgroud)
这是查询名为'GetQuestionnaireData()'的内存中的IEnumerable集合
但是从上面的方法签名我使用Expression> predicateFunc.然后我通过predicateFunc.Compile在linq查询的where子句中使用它.
我的问题是:是否可以在不使用Expression和delegate.compile的情况下将Func委托传递给linq where子句?
也许这可能是使用@ IEnumerable.Select(somePredicateFunc)?
如何使用系数数组建立表达式并将其转换为Func<int, double>?有比表达树更好的方法吗?
我有一个不可变的序列类型,使用一个Func<int, double> formula用于为序列A生成术语An的构造.我开始构建一个辅助类,用一些简单的参数构造常见的数学公式:
public static Sequence CreateLinearSequence (double m, double b)
{ return new Sequence (n => m * n + b); }
Run Code Online (Sandbox Code Playgroud)
我为常量序列,对数和简单多项式(线性,二次,三次和四次)构建了标准方法,但我想扩展它以使用params关键字支持任意数量的项.
这是我的方法:
public static Sequence CreatePolynomialSequence (params double[] coeff)
{
Expression<Func<int, double>> e = x => 0;
double pow = 0;
for (int i = coeff.Length - 1; i >= 0; i--)
{
double c = coeff[i];
var p = Expression.Parameter (typeof (int), "x");
e = Expression.Lambda<Func<int, …Run Code Online (Sandbox Code Playgroud) func ×10
c# ×7
.net ×2
delegates ×1
expression ×1
f# ×1
generics ×1
haskell ×1
jestjs ×1
linq ×1
math ×1
nsubstitute ×1
polynomials ×1
reactjs ×1
reflection ×1
sqlalchemy ×1
where ×1