我正在创建一个 React / .NET Core 项目,其中一个页面显示当前日期和时间。我的控制器类如下:
namespace TestingReactDotNet.Controllers {
[Route ("api/[controller]")]
public class DayTodayController : Controller {
[HttpGet, Route ("GetDate")]
public string GetDate () {
var info = $"Today is {DateTime.Now.ToString("dddd, dd MMMM yyyy")} and the time is {DateTime.Now.ToString("hh:mm tt")}";
return info;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试对此进行单元测试并模拟日期和时间,但我正在努力做到这一点(我已经查看了其他 Stack Overflow 问题并在 Google 上搜索,但我仍然遇到问题)。本来想用的,Moq但是好像不行啊?
这是我到目前为止的测试内容:
namespace TestingReactDotNetTests
{
public class DayTodayTests
{
[Test]
public void ReturnsDateAndTime()
{
var controller = new DayTodayController();
string result = controller.GetDate();
string expected = "Today is …Run Code Online (Sandbox Code Playgroud) 我正在学习 React 钩子并有以下代码:
import React, { useState, useEffect } from "react";
import "./App.css";
function App() {
const [count, setCount] = useState(0);
const [person, setPerson] = useState([]);
useEffect(() => {
getPerson();
}, []);
const getPerson = async () => {
const response = await fetch("https://api.randomuser.me");
const data = await response.json();
setPerson(data.results);
};
return (
<div className="App">
<p>You clicked {count} times</p>
<button onClick={() => getPerson(), setCount(count + 1)}>Click Me</button>
<div>{person.map(person => person.name.first)}</div>
</div>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
当我单击“单击我”按钮时,我希望计数器更新并进行 API 调用以获取随机人员。单独地,这两部分代码都可以工作。但是,当我尝试同时执行这两项操作时,我收到此错误:Too …
我想更好地理解组合而不是继承。我看了一些教程,它们很好地解释了这些概念,但使用的是我更熟悉的语言,例如 Python。然而,我目前正在学习 C#,正在努力运用我在该主题上学到的知识,想知道是否有人可以提供帮助。这是我的代码:
using System;
namespace Composition
{
class Program
{
static void Main(string[] args)
{
SuperBot steven = new SuperBot(new Dog(),new Robot());
steven.Bark();
}
}
class Dog
{
public void Bark()
{
Console.WriteLine("Woof");
}
}
class Robot
{
public void Move()
{
Console.WriteLine("I'm moving!");
}
}
class CleanRobot
{
public void Clean()
{
Console.WriteLine("Just keep dusting, just keep dusting");
}
}
class SuperBot
{
// can clean, move and bark
public Dog o1;
public Robot o2;
public CleanRobot o3; …Run Code Online (Sandbox Code Playgroud) 我有一个关于Ruby返回值的问题,这让我感到困惑。
我编写了一个方法,该方法将数组作为参数并将数组格式化为列表,如下所示:
def list(array)
array.each { |name, age| puts name + " is #{age} years old" }
end
Run Code Online (Sandbox Code Playgroud)
假设数组为[["Amy", 6], ["Tabitha", 5], ["Marcus", 9]]。
我希望此列表方法返回do / end块中的字符串,而不返回数组。但是,返回值始终是一个数组。
我尝试将块分配给变量并返回该变量,但是它不起作用。我也尝试用putsreturn 替换,但是在第一次迭代后退出该方法。似乎无法解决问题所在?
抱歉,这是一个非常愚蠢的问题-我之前从未遇到过。
任何输入非常感谢,谢谢!:)
我是 C# 新手,正在尝试找出如何计算字符串中重复项的数量。输入和输出示例如下:
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码如下:
using System;
using System.Collections;
using System.Linq;
public class Kata
{
public static int DuplicateCount(string str)
{
Stack checkedChars = new Stack();
Stack dupChars = new Stack();
str = str.ToLower();
for (int i=1; i < str.Length; i++) {
var alreadyCounted …Run Code Online (Sandbox Code Playgroud) 我有一系列哈希,如下所示:
details = [
{:name => "Alice", :age => 20},
{:name => "Ted", :age => 25},
{:name => "Poppy", :age => 33},
{:name => "Amy", :age => 20},
{:name => "Ted", :age => 90},
{:name => "Amy", :age => 22},
{:name => "Ted", :age => 23}
]
Run Code Online (Sandbox Code Playgroud)
我希望能够排序,以便可以根据每个人的名字出现的次数进行排序。例如,输出可能是:
"Ted, Ted, Ted, Amy, Amy, Alice, Poppy"
谁能帮忙吗?:)
谢谢
我正在尝试对数字的每个数字求平方,并返回一个包含所有平方值的整数。例如,如果输入的整数是9119,811181则将出来,因为9的平方是81,而1的平方是1。到目前为止,我的尝试是:
using System;
using System.Collections.Generic;
public class Kata
{
public static int SquareDigits(int n) {
String inputNums = n + "";
String[] digits = inputNums.Split("");
String outputNums = "";
foreach (string s in digits) {
int i = Int32.Parse(s);
var outputNum = (i * i);
outputNums += (outputNum);
}
return Int32.Parse(outputNums);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不断收到此错误:
Test Failed
Expected: 811181
But was: 83156161
Run Code Online (Sandbox Code Playgroud)
谁能帮我了解我哪里出了问题?我试图将整数转换回字符串,以尝试将它们连接在一起,但未将它们加在一起,但是我似乎无法得出正确的值。我对C#还是比较陌生,因此可以提供任何帮助-谢谢:)
c# ×4
arrays ×2
ruby ×2
asp.net-core ×1
composition ×1
hash ×1
moq ×1
nunit ×1
react-hooks ×1
reactjs ×1
unit-testing ×1