工厂女孩,默认特质中的关联,但在特质传递时让它无关联

时间:2015-05-11 10:25:12

标签: ruby-on-rails rspec factory-bot

我正在使用rspec测试@Override protected void onPostExecute(final Boolean isReachable) { if (isReachable) { // diaplay website first setContentView(mWebView); mWebView.loadUrl(mURLString); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // cancel the dialog and website is also launched dialog.dismiss(); } },2000 /* delay of 2 seconds */); } else { createAndShowNetworkDialog(); } } 模型,该模型有多个#include <sys/types.h> #include <stdio.h> int cstring_cmp(const void *a, const void *b) { const char **ia = (const char **)a; const char **ib = (const char **)b; return strcasecmp(*ia, *ib); } int Test() { char nameArray[4][10]={"test","alpha","Hyper","city"}; // int nElem = sizeof(nameArray)/sizeof(char *); int index = 0; //printf("nElem =%d\n", nElem); for(index=0; index < 4; index++) { printf("-> %s\n", nameArray[index]); } qsort( &nameArray[0], 4, sizeof(nameArray[0]), cstring_cmp); printf("After sort\n"); for(index=0; index < 4; index++) { printf("-> %s\n", nameArray[index]); } return 0 ; }

我正在使用工厂女孩来制作我的测试环境,我希望默认公司注册一些地址,而我希望公司的特定情况具有特征而没有任何Company

以下是我现在在工厂所做的事情:

Addresses

如何制作工厂,以便在使用特征Address时根本不创建地址?

谢谢,

1 个答案:

答案 0 :(得分:6)

FactoryGirl的transient attributes可能就是您所需要的:

FactoryGirl.define do
  factory :company do
    transient do
      create_address true
    end

    sequence(:name) { |n| "company #{n}" }
    description     { "description #{name}" }

    trait :no_address do
      transient do 
        create_address false
      end
    end

    after(:create) do |company, evaluator|
      create_list(:address, 3, company: company) if evaluator.create_address
    end
  end
end