RSpec 和奇怪的测试结果

发布于 2024-10-28 03:08:47 字数 2043 浏览 0 评论 0原文

我正在尝试制作一个简单的应用程序。当我在浏览器中测试它时,一切正常。然而,当我尝试使用 RSpec (2.5) 运行一些测试时,它在为控制器创建测试时失败了。

这是我的创建方法:

def create
 @website = Website.new(params[:website])
 if @website.save

   flash[:notice] = "Website created."
   redirect_to(:action => 'list')
 else
   render('new')
 end
end

控制器测试:

describe WebsitesController do
  render_views
  .
  .
  .
  describe "POST 'create'" do
    before(:each) do
      @attr = { :adres => "www.excc.pl", :opis => "aaa "*22, :tagi => "aaa aaa aaa",
                :preview => File.new(Rails.root + 'spec/fixtures/rails.png'),
                :preview_mini => File.new(Rails.root + 'spec/fixtures/rails.png')}
    end
    describe "success" do
      it "should have the right title" do
        response.should have_selector("title", :content=>"Lista witryn w portfolio")
      end
    end
  .
  .
  .

该测试的结果:

  1) WebsitesController POST 'create' should have the right title
     Failure/Error: response.should have_selector("title", :content=>"Lista witryn    w portfolio")
     expected following output to contain a <title>Lista witryn w portfolio</title> tag:
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
     # ./spec/controllers/websites_controller_spec.rb:34:in `block (4 levels) in

websites_controller_spec.rb:34 指的是创建方法

但是,此测试正确通过(对于不正确的数据,应将其重定向回具有指定标题的“新”站点):

it "should have the right title" do
    post :create, :website => @attr.merge(:adres => "")
    response.should have_selector("title", :content=>"Dodaj stronę WWW")
end

第二个问题是... 曾经有一段时间,我得到了这样的测试结果:

<html><body>You are being <a href="http://test.host/websites/list">redire cted</a>.</body></html>

……这让我抓狂了一段时间,直到我做了某件事(我真的不知道是什么),然后它就消失了。然而,当我想到它将来可能会再次出现并毁掉我的幸福时,我感到非常害怕。

任何对此的想法将不胜感激。

I'm trying to make a simple app. When Im testing it in browser everytyhing works just fine. Howerver, when I try to run some tests with RSpec (2.5) it fails when it comes to :create test for controller.

Here's my create method:

def create
 @website = Website.new(params[:website])
 if @website.save

   flash[:notice] = "Website created."
   redirect_to(:action => 'list')
 else
   render('new')
 end
end

The controller test:

describe WebsitesController do
  render_views
  .
  .
  .
  describe "POST 'create'" do
    before(:each) do
      @attr = { :adres => "www.excc.pl", :opis => "aaa "*22, :tagi => "aaa aaa aaa",
                :preview => File.new(Rails.root + 'spec/fixtures/rails.png'),
                :preview_mini => File.new(Rails.root + 'spec/fixtures/rails.png')}
    end
    describe "success" do
      it "should have the right title" do
        response.should have_selector("title", :content=>"Lista witryn w portfolio")
      end
    end
  .
  .
  .

The result of this test:

  1) WebsitesController POST 'create' should have the right title
     Failure/Error: response.should have_selector("title", :content=>"Lista witryn    w portfolio")
     expected following output to contain a <title>Lista witryn w portfolio</title> tag:
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
     # ./spec/controllers/websites_controller_spec.rb:34:in `block (4 levels) in

websites_controller_spec.rb:34 refers to create method

However, this test is passed correctly (for incorrect data it should be redirected back to 'new' site with specified title):

it "should have the right title" do
    post :create, :website => @attr.merge(:adres => "")
    response.should have_selector("title", :content=>"Dodaj stronę WWW")
end

The second problem is...
There was a time when I've got a test result like this:

<html><body>You are being <a href="http://test.host/websites/list">redire cted</a>.</body></html>

... which was causing me to pull my hair out for some time until I've done sth (I don't really know what) and it was gone. Yet, it makes me scared like hell when I think that it can come back in future an ruin my happiness.

Any thoughts on this would be greatly appreciated.

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

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

发布评论

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

评论(1

枫林﹌晚霞¤ 2024-11-04 03:08:47

很难知道这里问的是什么,但我相信问题在于你没有设定成功/失败的条件。如果我理解正确,当您传入空白 :adres 属性时,保存应该失败,并且页面应该呈现 list 操作。因此,您希望对 create 方法进行存根并根据预期结果返回 true 或 false:

it "succeeds" do
  @website = mock_model(Website,:save=>true)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # redirects
  response.should have_selector("etc etc")
end


it "fails" do
  @website = mock_model(Website,:save=>false)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # renders 'new'
  response.should_not have_selector("etc etc")
end

应在模型规范中执行参数有效性的测试:

@website = Website.new(:adres=>"")
@website.should_not be_valid

It's hard to know what is being asked here, but I believe the issue is that you are not setting the conditions for success/failure. If I understand correctly, when you pass in an blank :adres attribute, the save should fail and the page should render the list action. So you want to stub the create method and return true or false depending on the expected result:

it "succeeds" do
  @website = mock_model(Website,:save=>true)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # redirects
  response.should have_selector("etc etc")
end


it "fails" do
  @website = mock_model(Website,:save=>false)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # renders 'new'
  response.should_not have_selector("etc etc")
end

Testing of the validity of the parameters should be performed in the model spec:

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