在 Rails 中创建和测试默认记录
我正在为我的网站创建一个用户身份验证系统。我想添加一个默认用户,这样如果我创建一个新站点或重置数据库,我仍然可以登录(并更改默认用户详细信息)。
我在迁移中有以下代码。对于开发数据库来说,一切似乎都工作正常,但在测试时,我添加的默认用户被删除,而是加载了夹具中的数据。有没有办法为此添加单元测试?
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :hashed_password
t.string :salt
t.timestamps
end
User.add_user("mike", "password")
end
def self.down
drop_table :users
end
end
I'm creating a user authentication system for my site. I want to add a default user, so that if I create a new site, or reset the database I will still be able to log in (and change the default user details).
I have the code below in the migration. All seems to be working fine for the development database, but when it come to testing the default user I'm adding is removed and the data from the fixture is loaded instead. Is there any way to add unit testing for this?
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :hashed_password
t.string :salt
t.timestamps
end
User.add_user("mike", "password")
end
def self.down
drop_table :users
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈建议在 自定义 rake 中执行此类“种子数据”任务,并且在正常的单元测试装置中有不同的数据。
I highly recommend doing this sort of "seed data" inside a custom rake task, and have different data inside normal unit test fixtures.