2

我正在尝试将以下规范转换为新的期望语法,有人可以帮忙吗?

describe PostPolicy do
  subject { PostPolicy }

  permissions :create? do
    it "denies access if post is published" do
      should_not permit(User.new(:admin => false), Post.new(:published => true))
    end

    it "grants access if post is published and user is an admin" do
      should permit(User.new(:admin => true), Post.new(:published => true))
    end

    it "grants access if post is unpublished" do
      should permit(User.new(:admin => false), Post.new(:published => false))
    end
  end
end

我试过了,但它没有用,因为permit()返回了一个匹配器—— RSpec::Matchers::DSL::Matcher

specify { expect(permit(@user, @post)).to be_true }
4

2 回答 2

2

您必须subject显式调用 ,因为隐式接收器仅适用于should. 更多信息在这里这里

在您的示例中,这应该有效:

describe PostPolicy do
  subject { PostPolicy }

  permissions :create? do
    it "denies access if post is published" do
      expect(subject).not_to permit(User.new(:admin => false), Post.new(:published => true))
    end

    it "grants access if post is published and user is an admin" do
      expect(subject).not_to permit(User.new(:admin => true), Post.new(:published => true))
    end

    it "grants access if post is unpublished" do
      expect(subject).not_to permit(User.new(:admin => false), Post.new(:published => false))
    end
  end
end
于 2013-10-09T12:37:34.460 回答
0

另一种选择是使用隐式主题语法。

describe PostPolicy do
  subject { PostPolicy }

  permission :create? do
    it { is_expected.not_to permit(User.new(admin: false), Post.new(published: true)) }
  end
end

is_expected简单地调用expect(subject). 它使一个衬里更方便一些。

于 2015-09-07T22:11:55.867 回答