在创建新的 Rails 应用程序时,如何告诉 Rails 使用 RSpec 而不是测试单元?

发布于 2024-11-24 14:34:23 字数 280 浏览 2 评论 0原文

我安装了 test-unitrspec (以及 -core-expectations-模拟 和 -rails 版本 2.6.x)。当我运行命令 rails new foo 时,它使用 test-unit 生成测试存根文件,而不是 rspec

有没有一个选项可以让我告诉rails 使用 rspec 来生成测试?

I have test-unit installed and rspec installed (along with -core, -expectations, -mocks and -rails version 2.6.x). When I run the command rails new foo, it uses test-unit to generate the test stub files instead of rspec.

Is there an option where I can tell rails to use rspec to generate the tests instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

软甜啾 2024-12-01 14:34:23

以下内容应该有效:

在命令行:

rails new MYAPP -T # The -T option tells rails not to include Test::Unit

在 Gemfile 中:

gem 'rspec-rails'

在命令行:

bundle install
rails g rspec:install

The following should work:

at command line:

rails new MYAPP -T # The -T option tells rails not to include Test::Unit

in Gemfile:

gem 'rspec-rails'

at command line:

bundle install
rails g rspec:install
云裳 2024-12-01 14:34:23

创建新的 Rails 应用程序:

rails new <app_name> -T

或者从现有应用程序中删除 test 目录:

rm -rf test/

在 Gemfile 中创建一个条目:

gem 'rspec-rails'

从命令行安装 gem

$ bundle install

从命令行将 rspec 安装到您的应用程序中:

$ rails g rspec:install

现在您的Rails 应用程序使用 RSpec 而不是测试单元。

Create your new rails application as:

rails new <app_name> -T

Or remove your test directory from your existing application:

rm -rf test/

Make an entry in your Gemfile:

gem 'rspec-rails'

From the command line install the gem

$ bundle install

From the command line install rspec into your application:

$ rails g rspec:install

Now your rails application uses RSpec instead of test-unit.

童话 2024-12-01 14:34:23

创建 Rails 应用程序后,

rails new <app_name> -T  # to exclude Test::Unit

按以下方式将 RSpec gem 添加到 Gemfile 中:

group :development, :test do
  gem "rspec-rails"
end

在命令行中写入:

bundle install  # this will install the missing gems

现在您需要通过运行来安装 RSpec:

rails generate rspec:install

这将生成以下文件:

create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb

我强烈建议通读所有 spec_helper 和rails_helper 注释可以很好地理解每个选项的作用。

一切设置完毕后,您可以运行所有测试:

bundle exec rspec

您可以在 https 上阅读有关推荐的spec_helper和rails_helper配置的更多信息://kolosek.com/rails-rspec-setup

Once you created your rails application with:

rails new <app_name> -T  # to exclude Test::Unit

Add the RSpec gem to your Gemfile in the following way:

group :development, :test do
  gem "rspec-rails"
end

In Command line write:

bundle install  # this will install the missing gems

Now you need to install RSpec by running:

rails generate rspec:install

This will generate the following files:

create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb

I strongly recommended to read through all spec_helper and rails_helper comments to get a good understanding of what each option does.

Once everything is set you can run all your tests with:

bundle exec rspec

You can read more about the recommended spec_helper and rails_helper configurations on https://kolosek.com/rails-rspec-setup.

你爱我像她 2024-12-01 14:34:23

我是一名新开发人员,我刚刚制作了一个 Rails 标志(-rspec)来解决 OP 的问题。它摆脱了 Test::Unit 并使用 bash 脚本插入了rails-rspec gem。可以修改该脚本,以通过自动添加 ruby​​racer gem 或创建自定义标志和 gemset 来帮助 Linux 开发人员。 (也许专门去那条宝石线并删除评论)

这是要点&&我希望这对那里的人有帮助。
https://gist.github.com/MTen/8310116

I'm a new developer and I just made a rails flag (-rspec) to address OP's problem. It gets rid of Test::Unit and inserts the rails-rspec gem with a bash script. The script can be modified to help linux developers by automatically adding therubyracer gem or create custom flags and gemsets. (maybe specifically going to that gem line and deleting the comment)

Here's the gist && I hope this helps someone out there.
https://gist.github.com/MTen/8310116

凌乱心跳 2024-12-01 14:34:23

使用 rails CLI 没有本地方法可以做到这一点。生成一个不带 test-unit 的新项目:

rails new Rails-app --skip-test

然后按照 rspec 说明将其集成到新的 Rails 应用程序。

There's no native way to do this with the rails CLI. Generate a new project without test-unit:

rails new rails-app --skip-test

And then follow rspec instructions to integrate it into the new Rails app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文