GVKun编程网logo

Laravel HTTP 测试 - 确保 JSON 响应在数组中具有特定值(laravel接收json)

9

本文将为您提供关于LaravelHTTP测试-确保JSON响应在数组中具有特定值的详细介绍,我们还将为您解释laravel接收json的相关知识,同时,我们还将为您提供关于angularjs–测试具有

本文将为您提供关于Laravel HTTP 测试 - 确保 JSON 响应在数组中具有特定值的详细介绍,我们还将为您解释laravel接收json的相关知识,同时,我们还将为您提供关于angularjs – 测试具有JSON请求体的Laravel控制器、AngularJS:筛选不具有特定值的ng-options、c# – 获取从抽象类继承并在属性中具有特定值的类的实例、C++程序在数组中搜索特定值的实用信息。

本文目录一览:

Laravel HTTP 测试 - 确保 JSON 响应在数组中具有特定值(laravel接收json)

Laravel HTTP 测试 - 确保 JSON 响应在数组中具有特定值(laravel接收json)

因为您断言包含通配符的路径,您将获得每个匹配项的值(此函数在后台使用 the data_get() helper。)您需要构建一个具有相同元素的数量。可能是这样的:

public function should_only_return_data_that_are_running()
{
    $response = $this->getJson('/api/v2/data');
    $test = array_fill(0,count($response->data),'Running');
    $response->assertJsonPath('data.*.status',$test);
}

angularjs – 测试具有JSON请求体的Laravel控制器

angularjs – 测试具有JSON请求体的Laravel控制器

我正在为一个Laravel控制器编写一个PHPunit测试,期望使用 JSON格式的主体发布请求.

控制器的简化版本:

class Account_Controller extends Base_Controller
{
    public $restful = true;

    public function post_login()
    {
        $credentials = Input::json();
        return json_encode(array(
            'email' => $credentials->email,'session' => 'random_session_key'
        ));
    }
}

目前我有一个测试方法正确地发送数据作为urlencoded表单数据,但我无法解决如何发送数据作为JSON.

我的测试方法(我在编写测试时使用github gist here)

class AccountControllerTest extends PHPUnit_Framework_TestCase {
    public function testLogin()
    {
        $post_data = array(
            'email' => 'user@example.com','password' => 'example_password'
        );
        Request::foundation()->server->set('REQUEST_METHOD','POST');
        Request::foundation()->request->add($post_data);
        $response = Controller::call('account@login',$post_data);
        //check the $response
    }
}

我在前端使用angularjs,默认情况下,发送到服务器的请求是JSON格式.我不想改变它发送一个urlencoded形式.

有谁知道我如何编写一个测试方法,为控制器提供一个JSON编码体?

这样做很简单.您可以将Input :: $json属性设置为要作为post参数发送的对象.请参阅下面的示例代码
$data = array(
        'name' => 'sample name','email' => 'abc@yahoo.com',);

 Input::$json = (object)$data;

 Request::setMethod('POST');
 $response = Controller::call('client@create');
 $this->assertNotNull($response);
 $this->assertEquals(200,$response->status());

我希望这可以帮助你测试用例

更新:原始文章可在这里http://forums.laravel.io/viewtopic.php?id=2521

AngularJS:筛选不具有特定值的ng-options

AngularJS:筛选不具有特定值的ng-options

我想筛选一个像这样的选择:

<select ng-model="test" ng-options="c as c.label group by c.type for c in columns
| filter:{c.type:'!field'} | filter:{c.type:'!map'}"></select>

编辑:添加列模型:

Columns = [
{
    name: "name",label: "Label",info: "Information displayed in help",type: "type",view: "html template",style: "min-width: 10em;",show: true
},{
 ...
}
];

列用于多种用途,并且为了优化我的代码,我需要将其也包含在Select中,但没有类型为“字段”或“映射”的条目

但是,我可以从所有内容中进行选择,甚至包括类型为“字段”和“地图”的条目。有没有一种清洁的方法?

c# – 获取从抽象类继承并在属性中具有特定值的类的实例

c# – 获取从抽象类继承并在属性中具有特定值的类的实例

我正在一家工厂根据两个标准创建具体实例:

1)该类必须从特定的抽象类继承

2)类必须在重写属性中具有特定值

我的代码看起来像这样:

public abstract class CommandBase
{
    public abstract string Prefix { get; }
}

public class PaintCommand : CommandBase
{
    public override string Prefix { get; } = "P";
}

public class WalkCommand : CommandBase
{
    public override string Prefix { get; } = "W";
}

class Program
{
    static void Main(string[] args)
    {
        var paintCommand = GetInstance("P");
        var walkCommand = GetInstance("W");  

        Console.ReadKey();
    }

    static CommandBase GetInstance(string prefix)
    {
        try
        {            
            var currentAssembly = Assembly.GetExecutingAssembly();
            var concreteType = currentAssembly.GetTypes().Where(t => t.IsSubclassOf(typeof(CommandBase)) &&
                                                                     !t.IsAbstract &&
                                                                     t.GetProperty("Prefix").GetValue(t).ToString() == prefix).SingleOrDefault();

            if (concreteType == null)
                throw new InvalidCastException($"No concrete type found for command: {prefix}");

            return (CommandBase)Activator.CreateInstance(concreteType);
        }
        catch (Exception ex)
        {            
            return default(CommandBase);
        }
    }
}

我收到错误:

{System.Reflection.TargetException: Object does not match target type. at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object[] parameters,CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,CultureInfo culture)

解决方法

更简洁的方法是定义自己的属性来存储前缀.

[AttributeUsage(AttributeTargets.Class)]
public class CommandAttribute : Attribute
{
    public String Prefix { get; set; }

    public CommandAttribute(string commandPrefix)
    {
        Prefix = commandPrefix;
    }
}

然后像这样使用它们:

[CommandAttribute("P")]
public class PaintCommand : CommandBase
{}

[CommandAttribute("W")]
public class WalkCommand : CommandBase
{}

反思:

static CommandBase GetInstance(string prefix)
{       
    var currentAssembly = Assembly.GetExecutingAssembly();
    var concreteType = currentAssembly.GetTypes().Where(commandClass => commandClass.IsDefined(typeof(CommandAttribute),false) && commandClass.GetCustomAttribute<CommandAttribute>().Prefix == prefix).FirstOrDefault();

    if (concreteType == null)
        throw new InvalidCastException($"No concrete type found for command: {prefix}");

    return (CommandBase)Activator.CreateInstance(concreteType);
}

C++程序在数组中搜索特定值

C++程序在数组中搜索特定值

c++程序在数组中搜索特定值

假设我们有一个数组“arr”,其中包含 n 个已排序的整数值。我们还得到了一个大小为 q 的数组“query”,我们必须告诉“query”中的值是否存在于给定数组“arr”中。如果查询中的值存在于 arr 中,则打印“存在”以及该值所在的位置。否则,我们打印“不存在”并打印 arr 中的位置,其中最小值大于中的值查询位于。我们必须记住,数组是 1 索引的。

因此,如果输入类似于 n = 8, arr = {1, 2, 3, 4, 7, 9, 12, 15} , q = 3, query = {1, 5, 8},则输出为

Present 1
Not present 5
Not present 6
登录后复制
登录后复制

查询的第一个值出现在 arr 的位置 1 中。

查询的第二个值出现在 arr 中。大于query中的值的最小值的位置是5。

立即学习“C++免费学习笔记(深入)”;

点击下载“修复打印机驱动工具”;

同理,arr中也没有query的第三个值。大于它的值位于 arr 的位置 6。

为了解决这个问题,我们将按照以下步骤操作 -

  • 定义一个数组值
  • 对于初始化 i := 0,当 i < n 时,更新(将 i 增加 1),执行 -
    • 在值末尾插入 arr[i]
  • 初始化 i := 0,当 i < q 时,更新(将 i 增加 1),执行 -
    • idx : = (values中不小于query[i]的第一个元素的位置) -values中第一个元素的位置
    • 如果values[idx]与query[i]相同,则 -
      • print("存在")
    • 否则,
      • print("不存在")
    • print(idx + 1)

示例

让我们请参阅以下实现以获得更好的理解 -

#include <vector>
#include <iostream>
using namespace std;

void solve(int n, int arr[], int q, int query[]) {
   vector<int> values;
   for(int i = 0; i < n; i++){
      values.push_back(arr[i]);
   }
   for(int i = 0; i < q; i++) {
      int idx = lower_bound (values.begin(), values.end(),
      query[i]) - values.begin();
      if (values[idx] == query[i])
         cout << "Present ";
      else
         cout << "Not present ";
      cout << idx + 1 << endl;
   }
}
int main() {
   int input_arr[] = {1, 2, 3, 4, 7, 9, 12, 15};
   int query_arr[] = {1, 5, 8};
   solve(8, input_arr, 3, query_arr);
   return 0;
}
登录后复制

输入(标准输入)

int input_arr[] = {1, 2, 3, 4, 7, 9, 12, 15};
int query_arr[] = {1, 5, 8};
solve(8, input_arr, 3, query_arr);
登录后复制

输出

Present 1
Not present 5
Not present 6
登录后复制
登录后复制

以上就是C++程序在数组中搜索特定值的详细内容,更多请关注php中文网其它相关文章!

今天的关于Laravel HTTP 测试 - 确保 JSON 响应在数组中具有特定值laravel接收json的分享已经结束,谢谢您的关注,如果想了解更多关于angularjs – 测试具有JSON请求体的Laravel控制器、AngularJS:筛选不具有特定值的ng-options、c# – 获取从抽象类继承并在属性中具有特定值的类的实例、C++程序在数组中搜索特定值的相关知识,请在本站进行查询。

本文标签: