// fakePow.c
#include <math.h>
double pow(double b, double p)
{
return 6.0;
}
Run Code Online (Sandbox Code Playgroud)
//main.c
#include <stdio.h>
#include <math.h>
int twice(int x)
{
if (x <= 0)
return 0;
return x * 2;
}
int main()
{
int x = pow(10, 2);
int res = twice(x);
printf("%d\n", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
首先,我尝试了简单的编译,它不起作用,它返回200而不是12
gcc -c main.c
gcc -c fakePow.c
gcc main.o fakePow.o -o main
./main
200
Run Code Online (Sandbox Code Playgroud)
然后我用google找到了LD_PRELOAD,但它也不起作用,因为它返回200而不是12
gcc -c main.c
gcc -shared -o ./fakePow.so ./fakePow.c
LD_PRELOAD=./fakePow.so ./main
200
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些代码来帮助单元测试WCF服务.这些服务通过创建代理实例的facade类访问,然后调用代理方法并返回结果; 对于每种代理方法.我希望能够用创建真实服务或虚假服务的东西替换当前的创建代码.
我无法让它工作.我把它归结为以下内容:
using System.ServiceModel;
namespace ExpressionTrees
{
public interface IMyContract
{
void Method();
}
public class MyClient : ClientBase<IMyContract>, IMyContract
{
public MyClient()
{
}
public MyClient(string endpointConfigurationName)
: base(endpointConfigurationName)
{
}
public void Method()
{
Channel.Method();
}
}
public class Test
{
public TClient MakeClient<TClient>()
where TClient : ClientBase<IMyContract>, IMyContract, new()
{
return new MyClient("config");
// Error:
// Cannot convert expression of type 'ExpressionTrees.ServiceClient' to return type 'TClient'
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么即使MyClient该类派生ClientBase<IMyContract>并实现IMyContract,我也无法 …
我在检查一个对象是否是使用另一个对象实例的正确参数构造时遇到问题。在下面的示例中,我尝试B在 的实例中创建 的实例A。B我想检查实例内部构造函数中使用的参数A。当我运行下面的测试时,我得到:
AssertionError: assert None
[CPython36:setup:stdout] E + where None = <bound method NonCallableMock.assert_called_with of <MagicMock name='B' id='139968329210736'>>(4)
[CPython36:setup:stdout] E + where <bound method NonCallableMock.assert_called_with of <MagicMock name='B' id='139968329210736'>> = <MagicMock name='B' id='139968329210736'>.assert_called_with
Run Code Online (Sandbox Code Playgroud)
我不太确定我在这里做错了什么,并查看了其他堆栈溢出帖子,但无法解决我的问题。
b.py:
class B(object):
def __init__(self, x):
self.x = x
def square(self):
return x * x
Run Code Online (Sandbox Code Playgroud)
a.py:
from b import B
class A(object):
def foo(self):
b = B(4)
b.square()
Run Code Online (Sandbox Code Playgroud)
测试_a.py:
import unittest
from unittest.mock import patch
from a …Run Code Online (Sandbox Code Playgroud) 我写了一个函数:
func AllItems(w http.ResponseWriter, r *http.Request) {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
var items [] Item
db.Find(&items)
fmt.Println("{}", items)
json.NewEncoder(w).Encode(items)
}
Run Code Online (Sandbox Code Playgroud)
我想对此进行单元测试.理想情况下,单元测试意味着需要测试每个功能线.我不确定如何打开数据库连接,然后是否显示数据库的所有内容.我该如何测试这段代码?
此函数是简单CRUD应用程序的GET端点.代码在这里.
我正在尝试使用 Mockito 创建一个测试,但是我收到了 NullPointerException。
以下是会话 bean:
package edu.city.set.eia.citypress.beans;
import edu.city.set.eia.citypress.model.RegisUser;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class RegisUserFacade extends AbstractFacade<RegisUser> implements RegisUserFacadeLocal {
@PersistenceContext(unitName = "citypress_PU")
private EntityManager em;
@Override
public EntityManager getEntityManager() {
return em;
}
public void setEntityManager(EntityManager em) {
this.em = em;
}
public RegisUserFacade() {
super(RegisUser.class);
}
@Override
public RegisUser findByUsername(String username) {
Query query = this.getEntityManager().createQuery("select r from RegisUser r where username=:username");
query.setParameter("username", username);
RegisUser regisuser;
try {
regisuser = (RegisUser) …Run Code Online (Sandbox Code Playgroud) // BEGIN: external library
type realX struct {}
type realY struct {}
func (realX) Do() realY {
return realY{}
}
// END
type A struct {
a myX
}
type myY interface {}
type myX interface {
Do() myY
}
func foo (arg1 myY) {
}
func main() {
foo(realY{})
x := A{realX{}}
fmt.Println("Hello, playground")
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
cannot use realX literal (type realX) as type myX in field value:
realX does not implement myX (wrong type for Do method)
have …Run Code Online (Sandbox Code Playgroud)