Rspec多个控制器具有一个通用身份验证

时间:2017-10-25 20:18:23

标签: ruby-on-rails ruby rspec

大家好,我在spec / controller文件夹中的不同文件中对API进行了大量的控制器测试。对于每个规格我都有

before do
  authenticate_user(user, role)
end 

这是耗时的,因为我必须为每个测试创建一个用户和一个角色。我想在上层有同样的帮助程序,以便在相同的身份验证下执行我的所有控制器测试。

有没有人知道应该由谁来实现这个目标?

这也是一个选择,但用户下面有很多逻辑可能很困难且耗时。

感谢

1 个答案:

答案 0 :(得分:0)

您可以使用//define data pull for subnets with a depends on the resource for creating the subnets so there will be subnets present before data is calculated data "aws_subnet" "dmz_for_nat" { depends_on = ["aws_subnet.subnets"] filter { name = "tag:Name" values = ["${var.domain_short_name}-DMZ-B"] } } //Then reference that data for the subnet ID resource "aws_nat_gateway" "nat" { allocation_id = "${aws_eip.nat_gw.id}" subnet_id = "${data.aws_subnet.dmz_for_nat.id}" tags = "${merge(var.common_tags, map("Name", "${var.vpc_name_prefix}${var.domain_short_name}${var.region_short_name}-NAT" ))}" depends_on = ["aws_internet_gateway.igw","aws_subnet.subnets"] } before(:suite)来阻止对此方法的多次调用,例如

before(:all)

您也可以在before(:suite) do authenticate_user(user, role) end 找到它,例如:

spec/support/sign_in_helper.rb

并在module SignInHelper def sign_in before(:suite) do authenticate_user(user, role) end end end 使用它,例如:

spec_helper

之后,您将能够像顶级控制器规范中的宏一样使用它。

相关问题