在使用RSpec时是否有任何方法可以删除任何存根和模拟?
例:
RestClient.should_receive(:delete).with("http://www.example.com")
...
...
# this will remove the mocking of "should_receive" and
# restore the proper "delete" method on "RestClient".
RestClient.mocking_reset
Run Code Online (Sandbox Code Playgroud)
(mocking_reset
是我所需功能的虚构名称).
我知道有一种方法"unstub"可以重置"stubs"而不是"should_receive".
那么,有没有任何方法相当于"unstub"而是"should_receive"?
Panayotis
我正在研究Rails应用程序.我试图在全局范围内存根.
我正在做的是将其存储在RSpec配置中,在before(:suite)
块上,如下所示:
RSpec.configure do |config|
config.before(:suite) do
allow_any_instance_of(MyModel).to receive(:my_method).and_return(false)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,启动测试失败,并出现以下错误:
in `method_missing': undefined method `allow_any_instance_of' for #<RSpec::Core::ExampleGroup:0x00000008d6be08> (NoMethodError)
任何线索?我应该如何使用RSpec全局存根方法?
P.
ActiveRecord 3.2.14
我想在非Rails Ruby项目中使用ActiveRecord.我希望可以使用ActiveRecord定义的rake任务.我怎样才能做到这一点?
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the …
Run Code Online (Sandbox Code Playgroud) 我已经开始rem
在最近的项目中使用's来调整字体大小,然后使用px作为旧版IE的后备版.
我也设置了font-size
62.5%,html
所以我可以在样式表中更轻松地设置字体大小,然后我1.4rem
在主体上设置字体大小,所以没有样式的元素有font-size
至少14像素的基数,请参阅代码如下:
html { font-size: 62.5%; } /* font-size: 62.5% now means that 1.0 rem = 10px */
body { background: #fff; font-family: arial; font-size: 1.4rem; line-height: 1.6rem; }
Run Code Online (Sandbox Code Playgroud)
问题是,Chrome似乎以一种奇怪的方式处理这个问题...... Chrome似乎在初始页面加载时正确设置了字体大小,但在随后的刷新中,字体大小比它们应该更大.
查看FIDDLE(HTML复制如下以供将来参考)
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>Page Title</title>
</head>
<body>
<p>This is a test, this font should have font-size of 14px.</p>
<p>This is a test, this font should have font-size of 14px.</p>
<p>This is a test, this font …
Run Code Online (Sandbox Code Playgroud) 我想指示Capistrano加载在远程服务器上定义的环境变量.我怎样才能做到这一点?
似乎当我在环境变量中导出我的环境变量时.bashrc
,它们不会被Capistrano考虑在内.Capistrano似乎正在执行a /usr/bin/env
来创建执行远程命令的环境,但这似乎并没有从中加载环境变量.bashrc
.
我也告诉你我也在使用rvm-capistrano
(以防万一它可能有帮助).
任何线索?
我是FactoryGirl的新手,我正在尝试以下简单方案?
factory :female, :class => Gender do
code 'Female'
end
factory :male, :class => Gender do
code 'Male'
end
factory :papas, :class => Customer do
first_name 'Jim'
last_name 'Papas'
association :gender, :factory => :male, :strategy => :build
end
factory :dumas, :class => Customer do
first_name 'Mary'
last_name 'Dumas'
association :gender, :factory => :female, :strategy => :build
end
Run Code Online (Sandbox Code Playgroud)
然后在我的测试中:
create(:male)
create(:female)
create(:papas)
create(:dumas)
Run Code Online (Sandbox Code Playgroud)
请注意,Customer类具有关联belongs_to
Gender
类和验证规则,该规则gender_id
应该存在.我还对Gender
类的唯一性进行了验证.
在create(:papas)
上面的声明中,在我的测试中,我得到了Customer
将要创建的错误无效的错误,因为gender_id
是nil
.
如果删除了 …
我有这样一张桌子:
Table: p
+----------------+
| id | w_id |
+---------+------+
| 5 | 8 |
| 5 | 10 |
| 5 | 8 |
| 5 | 10 |
| 5 | 8 |
| 6 | 5 |
| 6 | 8 |
| 6 | 10 |
| 6 | 10 |
| 7 | 8 |
| 7 | 10 |
+----------------+
Run Code Online (Sandbox Code Playgroud)
获得以下结果的最佳SQL是什么?:
+-----------------------------+
| id | most_used_w_id |
+---------+-------------------+
| 5 | 8 |
| …
Run Code Online (Sandbox Code Playgroud) 假设我有一个类,如下所示:
class Book < ActiveRecord::Base
validates :title, :length => {:maximum => 10}
end
Run Code Online (Sandbox Code Playgroud)
有没有办法(安装宝石?)我可以让ActiveRecord根据最大长度自动截断值?
例如,当我写:
b = Book.new
b.title = "123456789012345" # this is longer than maximum length of title 10
b.save
Run Code Online (Sandbox Code Playgroud)
应该保存并返回true吗?
如果没有这种方式,你会如何建议我更普遍地面对这样的问题?
有没有办法在调用此方法时获取实现Ruby方法的方法列表?
例如:
def foo
puts "foo"
end
def foo2
foo
end
Run Code Online (Sandbox Code Playgroud)
我想知道,当调用"foo2"时,它调用第一个"foo"和第二个"puts"以及这些方法定义的相应文件.(如果"put"调用其他方法,我也想知道它们)
那可能吗?如果'是'怎么样?我可以说我的问题是找到方法依赖项.
使用具有Case Insensitive排序规则的MySQL DB假设Rails 3
发生了什么:
Rails允许您使用"唯一性"验证器验证模型的属性.但根据Rails文档,默认比较是CASE SENSITIVE.
这意味着在验证时它会执行如下SQL:
SELECT 1 FROM `Users` WHERE (`Users`.`email` = BINARY 'FOO@email.com') LIMIT 1
Run Code Online (Sandbox Code Playgroud)
对于拥有CI Collation数据库的我来说,这是完全错误的.它会认为'FOO@email.com'有效,即使在Users表中已有另一个用户'foo@email.com'的用户也是如此.换句话说,这意味着,如果应用程序的用户尝试使用电子邮件"FOO@email.com"创建新用户,则对于Rails,这将完全是VALID(默认情况下),并且INSERT将被发送到db.如果你没有碰巧在电子邮件上有唯一的索引,那么你很兴奋 - 插入行将没有问题.如果您碰巧拥有唯一索引,则会抛出异常.
好.Rails说:由于您的数据库具有不区分大小写的排序规则,因此请执行不区分大小写的唯一性验证.这是怎么做到的?它告诉您可以通过在特定属性验证器上设置":case_sensitive => false"来覆盖默认唯一性比较灵敏度.在验证时,它会创建以下SQL:
SELECT 1 FROM `Users` WHERE (LOWER(`Users`.`email`) = LOWER('FOO@email.com') LIMIT 1
Run Code Online (Sandbox Code Playgroud)
这是数据库表上的DISASTER您设计为在电子邮件字段上具有唯一索引的用户,因为它不使用索引,所以执行全表扫描.
我现在看到的是,LOWER
SQL函数是由插入UniquenessValidator
的ActiveRecord
(文件uniqueness.rb
,模块ActiveRecord
,模块Validations
级UniquenessValidator
).以下是执行此操作的代码段:
if value.nil? || (options[:case_sensitive] || !column.text?)
sql = "#{sql_attribute} #{operator}"
else
sql = "LOWER(#{sql_attribute}) = LOWER(?)"
end
Run Code Online (Sandbox Code Playgroud)
所以问题转到Rails/ActiveRecord而不是MySQL Adapter.
问题:有没有办法告诉Rails传递关于MySQL适配器的唯一性验证区分大小写的要求,而不是"聪明"地改变查询?或者 为了澄清而重复的问题:是否有另一种方法可以对属性实现唯一性验证(请小心,我只是不谈电子邮件,电子邮件就是一个例子),区分大小写和生成在相应列上使用简单唯一索引的查询?
这两个问题是等价的.我希望现在,我能让自己更清楚,以便获得更准确的答案.
ruby ×3
activerecord ×2
mocking ×2
mysql ×2
rspec ×2
associations ×1
attributes ×1
capistrano3 ×1
css ×1
factory-bot ×1
html ×1
max ×1
rake ×1
stub ×1
stubbing ×1