2014-11-24 103 views
0

在我们的Rails(3.2)的一个模块,还有条件include如何让R语句的语句总是返回true?

include UsersHelper if find_config_const('allow_sales_manage_customer_login') == 'true' 

这里find_config_const是表格检索找到allow_sales_manage_customer_login值的方法。使用rspec(版本2.14)进行测试时,模块UserHelper需要为include,因此find_config_const('allow_sales_manage_customer_login') == 'true'应始终返回true。 FactoryGirl不在此处工作,因为由FactoryGirl创建的条目在includefind_config_const('allow_sales_manage_customer_login') == 'true'之后加载为false。我们可以让find_config_const('allow_sales_manage_customer_login') == 'true'始终为rspec返回true(版本2.14)吗?

回答

1

答案是使用#stub或#should_receive存根方法。

你能告诉我find_config_const方法是在哪里定义的吗?

假设它是在SomeHelper中定义的。你可以这样做:

SomeHelper.any_instance.stub(:find_config_const).with('allow_sales_manage_customer_login').and_return(true) 
+0

@@ spiria,find_config_const在Authen :: AuthenUtility中定义。我们在(:each)之前放置以下内容:Authen :: AuthenUtility.any_instance.stub(:find_config_const).with('allow_sales_manage_customer_login')。and_return(true)。有NoMethod错误说未定义的Authen :: AuthenUtility:Module的any_instance。我们错过了什么? – user938363 2014-11-24 19:49:20

+0

你可以试试吗? (:'find_config_const').with('allow_sales_ma nage_customer_login')。and_return(true) – spiria 2014-11-25 13:46:09