多个期望具有相同预期输出的declorations

时间:2015-07-02 18:51:33

标签: javascript unit-testing testing mocha chai

我正在测试下面的函数shopify.formatRequestUrl,并且每次我都期望函数的输出将是相同的。有没有办法让这更干?

expect(shopify.formatRequestUrl("shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/shop/"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("shop.json"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/admin/shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/admin/shop.json"))
  .equal("https://"+settings.shop+"/admin/shop.json")

像这样: psudo code

expectAll[
  shopify.formatRequestUrl("shop"),
  shopify.formatRequestUrl("/shop"),
  shopify.formatRequestUrl("/shop/"),
  shopify.formatRequestUrl("shop.json"),
  shopify.formatRequestUrl("/admin/shop"),
  shopify.formatRequestUrl("/admin/shop.json")
]
  .equal("https://"+settings.shop+"/admin/shop.json")

这样的事情是否存在?

1 个答案:

答案 0 :(得分:0)

使用下划线和循环遍历所有参数的对象。

_.each([
  "shop",
  "/shop",
  "/shop/",
  "shop/",
  "shop.json",
  "/shop.json",
  "/shop.json/",
  "shop.json/",
  "/admin/shop",
  "/admin/shop/",
  "/admin/shop.json",
  "/admin/shop.json/",
  "admin/shop.json/",
], function(result){
  expect(shopify.formatRequestUrl(result))
    .equal("https://"+settings.shop+"/admin/shop.json")
})