首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec + shoulda:设置数据

rspec + shoulda:设置数据
EN

Stack Overflow用户
提问于 2016-04-24 19:28:12
回答 1查看 117关注 0票数 1

我有下面的测试。有三个it块。与其他两个不同的是,第一个不使用shoulda

如果我不将before块与post :create, product: attrs一起使用,那么第一个测试就会像预期的那样失败。但是如果我把before块放在那里,那么第一个测试就失败了,但是另外两个测试失败了。我对产品名称进行了唯一性验证,但这不应该是问题所在,因为我在工厂中使用序列。

我该怎么办?当同时存在rspec和setup时,我应该如何设置测试数据?

代码语言:javascript
复制
describe "when user logged in" do
  before(:each) do
    login_user #logged in user is available by calling @user
  end

  context "POST create" do
    context "with valid attributes" do
      let!(:profile) { create(:profile, user: @user) }
      let!(:industry) { create(:industry) }
      let!(:attrs) { attributes_for(:product, user_id: @user.id, industry_ids: [ industry.id ]).merge(
          product_features_attributes: [attributes_for(:product_feature)],
          product_competitions_attributes: [attributes_for(:product_competition)],
          product_usecases_attributes: [attributes_for(:product_usecase)]
        ) }

      it "saves the new product in the db" do
        expect{ post :create, product: attrs }.to change{ Product.count }.by(1)
      end

      #If I don't use this the 2 tests below fail. If I use it, then the test above fails.
      # before do
      #   post :create, product: attrs
      # end

      it { is_expected.to redirect_to product_path(Product.last) }
      it { is_expected.to set_flash.to('Product got created!') }
    end
  end
end

工厂

代码语言:javascript
复制
factory :product, class: Product do
  #name { Faker::Commerce.product_name }
  sequence(:name) { |n| "ABC_#{n}" }
  company { Faker::Company.name }
  website { 'https://example.com' }
  oneliner { Faker::Lorem.sentence }
  description { Faker::Lorem.paragraph }
  user
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-25 02:07:56

你不能两全其美。如果在before中执行正在测试的方法,则无法再次执行它以查看它是否更改了Product计数。如果您之前没有执行它,那么您必须在您的示例中执行它,因此不能使用is_expected一行程序格式。

有各种各样的选择。这里有一个将方法的执行集成到所有示例中的例子。

代码语言:javascript
复制
describe "when user logged in" do
  before(:each) do
    login_user #logged in user is available by calling @user
  end

  describe "POST create" do
    subject(:create) { post :create, product: attrs }
    context "with valid attributes" do
      let!(:profile) { create(:profile, user: @user) }
      let!(:industry) { create(:industry) }
      let!(:attrs) { attributes_for(:product, user_id: @user.id, industry_ids: [ industry.id ]).merge(
          product_features_attributes: [attributes_for(:product_feature)],
          product_competitions_attributes: [attributes_for(:product_competition)],
          product_usecases_attributes: [attributes_for(:product_usecase)]
        ) }

      it "saves the new product in the db" do
        expect{ create }.to change{ Product.count }.by(1)
      end

      it("redirects") { expect(create).to redirect_to product_path(Product.last) }
      it("flashes") { expect(create).to set_flash.to('Product got created!') }
    end
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36827856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档