Rails by Example 教程请求示例和 Capybara

发布于 2024-10-18 02:17:23 字数 910 浏览 1 评论 0原文

在 Michael Hartl 的 Rails on Rails 教程中,请求示例对响应进行断言。我安装了 cabybara 和牛排 gem 来创建验收测试。安装 capybara 后,请求示例配置为使用 capybara。水豚示例具有不同的语法,并且无法识别响应。
如何重置请求示例以作为 RSpec 示例运行?

测试错误:

4) Users signup failure should not make a new user  
    Failure/Error: click_button  
    wrong number of arguments (0 for 1)  
    # ./spec/requests/users_spec.rb:13  
    # ./spec/requests/users_spec.rb:7 

请求示例

describe "failure" do  
    it "should not make a new user" do  
        lambda do  
            visit signup_path  
            fill_in "Name", :with => ""  
            fill_in "Email", :with => ""  
            fill_in "Password", :with => ""  
            fill_in "Confirmation", :with => ""  
            click_button  
            response.should render_template('users/new')  
        end.should_not change(User, :count)  
    end
end

In the Rails on Rails Tutorial by Michael Hartl, the Request Examples make assertions on the response. I installed the cabybara and steak gem to create acceptance tests. After installing capybara, the requests examples are configured to use capybara. capybara examples have a different syntax and don't recognize the response.
How do I reset the Request Examples to run as RSpec example?

Test Error:

4) Users signup failure should not make a new user  
    Failure/Error: click_button  
    wrong number of arguments (0 for 1)  
    # ./spec/requests/users_spec.rb:13  
    # ./spec/requests/users_spec.rb:7 

Request Example

describe "failure" do  
    it "should not make a new user" do  
        lambda do  
            visit signup_path  
            fill_in "Name", :with => ""  
            fill_in "Email", :with => ""  
            fill_in "Password", :with => ""  
            fill_in "Confirmation", :with => ""  
            click_button  
            response.should render_template('users/new')  
        end.should_not change(User, :count)  
    end
end

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

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

发布评论

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

评论(2

对你的占有欲 2024-10-25 02:17:23

这可能与 rspec-rails request_example_group.rb 有关。

request_example_group.rb

如果水豚行被注释掉,那么请求示例不会默认为水豚。

capybara do
   include Capybara
end

在验收示例中,包括水豚。

require File.expand_path(File.dirname(__FILE__) + '/acceptance_helper')

feature "This Is My First Feature", %q{
    In order to ...
    As a ...
    I want to ...
} do

    include Capybara
    scenario "Scenario name" do
        visit signup_path
        fill_in "Name", :with => ""
        fill_in "Email", :with => ""
        fill_in "Password", :with => ""
        fill_in "Confirmation", :with => ""
        click_button "Sign up"
        page.has_content?('welcome to the sample app')
    end
end

我最终分叉了 rspec-rails 并注释掉了水豚行。
在 Gemfile 中,从 fork 安装的 gem。

何苦呢?我们想要白盒和黑盒测试。

This might have something to do with rspec-rails request_example_group.rb.

request_example_group.rb

If the capybara lines are commented out, then the Request Examples are not defaulted to capybara.

capybara do
   include Capybara
end

In the Acceptance examples, include capybara.

require File.expand_path(File.dirname(__FILE__) + '/acceptance_helper')

feature "This Is My First Feature", %q{
    In order to ...
    As a ...
    I want to ...
} do

    include Capybara
    scenario "Scenario name" do
        visit signup_path
        fill_in "Name", :with => ""
        fill_in "Email", :with => ""
        fill_in "Password", :with => ""
        fill_in "Confirmation", :with => ""
        click_button "Sign up"
        page.has_content?('welcome to the sample app')
    end
end

I ended up forking rspec-rails and commenting out the capybara lines.
In the Gemfile, the gem installed from the fork.

Why bother? We want both white box and black box testing.

做个少女永远怀春 2024-10-25 02:17:23

正如您在答案中正确指出的那样,如果安装了 rspec-rails,它将包含 Capybara。我认为他们假设包含 Capybara 不会有什么坏处,以防万一你需要它。这对你来说是个问题吗?换句话说,Capybara DSL 具体与什么冲突?

RSpec 邮件列表 上提出。)

(这可能也值得在 您发布的错误,似乎您只是缺少 click_button 方法。

As you correctly point out in your answer, rspec-rails will include Capybara if it is installed. I think they're assuming that it never hurts to include Capybara, just in case you need it. Is that a problem for you? In other words, what specifically is the Capybara DSL conflicting with?

(This might be also be worth bringing up on the RSpec mailing list.)

As for the error you posted, it seems that you are simply missing an argument to the click_button method.

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